Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  198] [ 6]  / answers: 1 / hits: 27497  / 13 Years ago, sat, december 17, 2011, 12:00:00

I want to insert a clickable image, that is, if any part of the image is clicked, it will either act like a button element, or more preferably, run a Javascript script.



I was thinking of inserting an image into a button element, but as far as I can tell, that would NOT just make the whole image into a button, but rather insert it into a button box.



How do I make a clickable image, that pulls up a script?


More From » html

 Answers
23

There are lots of ways to do this.



<img id=Img src=img.jpg />


Adding an onclick attribute:



<img id=Img src=img.jpg onclick=myFunction() />


Adding onclick event listener from script:



document.getElementById( Img ).onclick = function() {
// img clicked
};


Put image as button background



<input type=button style=background: url( img.jpg ) />

[#88512] Thursday, December 15, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikhilc

Total Points: 128
Total Questions: 100
Total Answers: 89

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;