- First, you will need to create an HTML file that includes an image that you want to make clickable.
- Next, add an id attribute to the image tag. This will make it easier to reference the image in your JavaScript code.
- Add an event listener to the image using JavaScript. The event listener should listen for a click event on the image.
- 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.