<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged onclick</title>
<link>https://coderlegion.com/tag/onclick</link>
<description>Powered by Question2Answer</description>
<item>
<title>[JavaScript] Img onclick</title>
<link>https://coderlegion.com/9701/javascript-img-onclick</link>
<description>&lt;h2&gt;Image onclick&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;'onclick'&lt;/code&gt; method is a &lt;strong&gt;great tool for&lt;/strong&gt; &lt;strong&gt;incorporating interactivity&lt;/strong&gt; into images. You may &lt;strong&gt;improve user experience&lt;/strong&gt; by allowing actions that are triggered when users click on an image using this way. This article will go through how to use the onclick technique to generate interactive pictures.&lt;/p&gt;
&lt;div id=&quot;toc-div&quot;&gt;
&lt;h4&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt;Table of Contents: &lt;a id=&quot;toc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;div id=&quot;toc-list&quot;&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;#why&quot; rel=&quot;nofollow&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;The onclick Event in JavaScript&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#use1&quot; rel=&quot;nofollow&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Alternative uses of onclick event for Visual Effects&lt;/span&gt;&lt;/a&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;#use2&quot; rel=&quot;nofollow&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Changing Image Source on Click&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#use3&quot; rel=&quot;nofollow&quot;&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;CSS Property Modification on Click&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;&lt;a href=&quot;#ref&quot; rel=&quot;nofollow&quot;&gt;References&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;section&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The &lt;code&gt;'onclick'&lt;/code&gt; method is a JavaScript &lt;strong&gt;event handler&lt;/strong&gt; that is applied to HTML elements, such as images, to &lt;strong&gt;execute a specified action&lt;/strong&gt; when the element is clicked. It allows you to define a JavaScript function or code snippet that will run when the image is clicked.&lt;/p&gt;
&lt;p&gt;This can be used to &lt;strong&gt;trigger a wide range of actions&lt;/strong&gt;, from opening pop-ups to toggling additional content or even navigating to a new page.&lt;/p&gt;
&lt;/section&gt;
&lt;section&gt;&lt;a id=&quot;why&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/section&gt;
&lt;section&gt;
&lt;h2&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;The onclick Event in JavaScript&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;When an image with the specified &quot;onclick&quot; attribute is clicked by the user, the JavaScript function defined in the &quot;onclick&quot; attribute will be executed.&lt;/p&gt;
&lt;p&gt;To get onclick event functionality in JavaScript, we must first write a function and then call it within the onclick, which is present on the image tag within the HTML. We've taken an image here, and when a user clicks on it, an alert message is generated. This will be demonstrated in the following example.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
title&amp;gt;Image onclick Example&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Click the image below:&lt;br&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;download.jpg&quot; alt=&quot;&quot; width=&quot;500&quot; height=&quot;300&quot;&gt;&lt;br&gt;
&lt;br&gt;
function showmessage() {&lt;br&gt;
alert('You clicked the image!');&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;

&lt;p class=&quot;MsoNormal&quot;&gt;If you run the above code, this is how it will look like in the web browser.&lt;/p&gt;
&lt;p class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt; &lt;/span&gt;&lt;a title=&quot;&quot;&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=15985640908290592941&quot; alt=&quot;&quot; width=&quot;500&quot; height=&quot;320&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you run the above code inside the web browser and click on the image, the image will show the alert message, as shown below.&lt;/p&gt;
&lt;p class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;mso-spacerun: yes;&quot;&gt; &lt;/span&gt;&lt;a title=&quot;&quot;&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=3324420427903186253&quot; alt=&quot;&quot; width=&quot;700&quot; height=&quot;300&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The HTML document contains an &lt;code&gt;'img'&lt;/code&gt; tag with the &lt;code&gt;'src'&lt;/code&gt; attribute pointing to an image file.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;'alt'&lt;/code&gt; attribute provides alternative text for the image.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;'onclick'&lt;/code&gt; attribute is set to &quot;showmessage()&quot;, which means the &lt;code&gt;'showmessage()'&lt;/code&gt; function will be triggered when the image is clicked.&lt;/li&gt;
&lt;li&gt;Inside the script tag, the &lt;code&gt;'showmessage()'&lt;/code&gt; function is defined to display an alert with the message &quot;You clicked the image!&quot;.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;div-pink&quot;&gt;&lt;span class=&quot;alert-header&quot;&gt;Caution&lt;/span&gt; Use &quot;onclick&quot; event with caution on parts that need significant actions, such as form submissions or damaging activities. Make sure the event doesn't affect how your web application should behave.&lt;/div&gt;
&lt;/section&gt;
&lt;section&gt;&lt;a id=&quot;use1&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/section&gt;
&lt;section&gt;
&lt;h2&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;Alternative uses of onclick event for Visual Effects&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;Another manner to use the &quot;onclick&quot; event is to &lt;strong&gt;trade the image source&lt;/strong&gt; or&lt;strong&gt; modify CSS residences&lt;/strong&gt; to provide visual effects when the image is clicked. For example, you may modify the image with a exceptional one or toggle a class to use a selected style.&lt;/p&gt;
&lt;section&gt;&lt;a id=&quot;use2&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/section&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;1. Changing Image Source on Click&lt;br&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;In this approach, we'll dynamically change the image source to enhance the user experience.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Image Swap Example &lt;br&gt;&lt;br&gt;&lt;br&gt;
/* CSS to give some styling to the image */&lt;br&gt;
img {&lt;br&gt;
width: 200px;&lt;br&gt;
border: 2px solid #ccc;&lt;br&gt;
cursor: pointer;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Click the image to change it:&lt;br&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img id=&quot;image&quot; src=&quot;download.jpg&quot; alt=&quot;image&quot;&gt;&lt;br&gt;
&lt;br&gt;
function changeImage() {&lt;br&gt;
var imageElement = document.getElementById(&quot;image&quot;);&lt;br&gt;
if (imageElement.src.endsWith(&quot;download.jpg&quot;)) {&lt;br&gt;
imageElement.src = &quot;images.jpg&quot; ;&lt;br&gt;
} else {&lt;br&gt;
imageElement.src = &quot;download.jpg&quot;;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;

&lt;p&gt;First off, we have defined an image element with the &lt;code&gt;id = &quot; image&quot;&lt;/code&gt;, which permits us to effortlessly enter and modify it after the usage of JavaScript. &lt;br&gt;The &lt;code&gt;'style'&lt;/code&gt; tag contains CSS tag that provide some simple styling to the image, which include a border and a pointer cursor to signify interactivity. &lt;br&gt;We've got an &quot;onclick&quot; characteristic inside the image tag that calls the JavaScript feature &lt;code&gt;changeImage()&lt;/code&gt; while the image is clicked. &lt;br&gt;In the JavaScript feature &lt;code&gt; 'changeImage()'&lt;/code&gt; , we first retrieve the photo element using &lt;code&gt;'document.GetElementById(&quot; image&quot;)'&lt;/code&gt; . &lt;br&gt;Subsequent, we check the cutting-edge source of the image the use of the &lt;code&gt;'.src'&lt;/code&gt; property. If the supply ends with&quot;image1.Jpg&quot;, we alternate it to&quot;image2.Jpg&quot;, and vice versa. &lt;br&gt;As a result, when the &lt;strong&gt;user clicks &lt;/strong&gt;on the photo, it will &lt;strong&gt;toggle&lt;/strong&gt; &lt;strong&gt;among two special images&lt;/strong&gt;, creating a visible impact&lt;/p&gt;
&lt;div class=&quot;div-faq&quot;&gt;&lt;span class=&quot;faq-header&quot;&gt;FAQ&lt;/span&gt; Q: Can I attach the onclick event to elements other than images?  &lt;br&gt;A: Yes, you can use the &quot;onclick&quot; event on various HTML elements, such as buttons, divs, and links. It allows you to trigger JavaScript functions when those elements are clicked.&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;section&gt;&lt;a id=&quot;use3&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/section&gt;
&lt;h3&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;2. CSS Property Modification on Click&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;Aside from changing the picture source, you can likewise utilize the &quot;onclick&quot; occasion to apply various styles to the image when it's clicked. You can, for instance, toggle a class on the image, and the class can have various CSS rules that change how it looks.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style=&quot;font-size: 14.0pt; mso-bidi-font-size: 11.0pt; line-height: 107%; font-family: 'Calibri',sans-serif; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-GB; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;&quot;&gt;Image Style Toggle Example&lt;/span&gt; &lt;br&gt;&lt;br&gt;&lt;br&gt;
/* CSS &lt;span style=&quot;font-size: 14.0pt; mso-bidi-font-size: 11.0pt; line-height: 107%; font-family: 'Calibri',sans-serif; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-GB; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;&quot;&gt;for default and clicked states &lt;/span&gt; */&lt;br&gt;
default-image {&lt;br&gt;
width: 200px;&lt;br&gt;
border: 2px solid #ccc;&lt;br&gt;
cursor: pointer; &lt;br&gt;
} &lt;br&gt;
.clicked-image {&lt;br&gt;
border: 2px solid #f00;&lt;br&gt;
box-shadow: 0 0 8px rgba(255, 0, 0, 0.5);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style=&quot;font-size: 14.0pt; mso-bidi-font-size: 11.0pt; line-height: 107%; font-family: 'Calibri',sans-serif; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-GB; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;&quot;&gt;Click the image to apply a style:&lt;/span&gt;&lt;br&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;download.jpg&quot; class=&quot;default-image&quot; alt=&quot;image&quot;&gt;&lt;br&gt;
&lt;br&gt;
function toggleStyle() {&lt;br&gt;
var imageElement = document.getElementById(&quot;image&quot;);&lt;br&gt;
imageElement.classList.toggle(&quot;clicked-image&quot;);&lt;br&gt;
}&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;

&lt;p&gt;In this example, two CSS classes are defined: &lt;code&gt;'.default-image'&lt;/code&gt; and &lt;code&gt;'.clicked-image'&lt;/code&gt;. The &lt;code&gt;'.default-image'&lt;/code&gt; class addresses the underlying style of the image, and the &lt;code&gt;'.clicked-image'&lt;/code&gt; class addresses the style when the image is clicked. When the onclick attribute in the image tags is clicked, the JavaScript function &lt;code&gt;'toggleStyle()'&lt;/code&gt; is called. Inside the &lt;code&gt;'toggleStyle()'&lt;/code&gt; function, we retrieve the image element utilizing &lt;code&gt;'document.getElementById(&quot;image&quot;)'&lt;/code&gt;. The &quot;clicked-image&quot; class can be added or removed from an image by utilizing the &lt;code&gt; '.classList.toggle()'&lt;/code&gt; method. The class is switched by this method; If it is there, it takes it out, and if it isn't there, it adds it. Consequently, the image will apply the &quot;&lt;strong&gt;clicked-image&lt;/strong&gt;&quot; class when the user clicks on it, &lt;strong&gt;changing the color of the border&lt;/strong&gt; and &lt;strong&gt;adding a box shadow&lt;/strong&gt; to create a visual effect. These alternative strategies demonstarte how the &quot;onclick&quot; event can be utilized to make dynamic and drawing in special visualizations by adjusting the picture source or applying various styles to the image when the user interacts with it.&lt;/p&gt;
&lt;/section&gt;
&lt;section&gt;&lt;a id=&quot;ref&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;
&lt;h2&gt;&lt;span style=&quot;font-family: arial,helvetica,sans-serif;&quot;&gt;References&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;For more reference, please use below links:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;HTML Living Standard:&lt;br&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://html.spec.whatwg.org/multipage/&quot; target=&quot;_new&quot;&gt;https://html.spec.whatwg.org/multipage/&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/section&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/9701/javascript-img-onclick</guid>
<pubDate>Wed, 14 Jan 2026 12:38:05 +0000</pubDate>
</item>
</channel>
</rss>