Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
  1. First, you will need to create an HTML file that includes an image that you want to make clickable.
  2. Next, add an id attribute to the image tag. This will make it easier to reference the image in your JavaScript code.
  3. Add an event listener to the image using JavaScript. The event listener should listen for a click event on the image.
  4. Inside the event listener function, use the console.log() method to display a message when the image is clicked.

Here is an example code snippet:

HTML:

<img src="image.jpg" id="clickable-image">

JavaScript:

const clickableImage = document.getElementById("clickable-image");

clickableImage.addEventListener("click", function() {
  console.log("Image clicked!");
});

When the image with id "clickable-image" is clicked, the console log message "Image clicked!" will be displayed.