Advertisement
gaz_lloyd

Untitled

Mar 10th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. one method - one function
  2.  
  3.  
  4. <!DOCTYPE html>
  5. <html>
  6. <body>
  7.  
  8. <h1>JavaScript Can Change Images</h1>
  9.  
  10. <img id="myImage" onclick="changeImage(this)" src="pic_bulboff.gif" width="100" height="180">
  11. <img id="myImage2" onclick="changeImage(this)" src="pic_bulboff.gif" width="100" height="180">
  12.  
  13. <p>Click the light bulb to turn on/off the light.</p>
  14.  
  15. <script>
  16. function changeImage(image) {
  17.     var wason = image.src.match("bulbon"); //if you don't want to preserve the previous functionality you can remove this
  18.     document.getElementById('myImage').src = "pic_bulboff.gif";
  19.     document.getElementById('myImage2').src = "pic_bulboff.gif";
  20.     if (!wason) //preserving the previous functionality
  21.       image.src = "pic_bulbon.gif";
  22. }
  23. </script>
  24.  
  25. </body>
  26. </html>
  27.  
  28.  
  29. ----
  30. other method - two functions
  31.  
  32.  
  33. <!DOCTYPE html>
  34. <html>
  35. <body>
  36.  
  37. <h1>JavaScript Can Change Images</h1>
  38.  
  39. <img id="myImage" onclick="changeImage(this)" src="pic_bulboff.gif" width="100" height="180">
  40. <img id="myImage2" onclick="changeImage2(this)" src="pic_bulboff.gif" width="100" height="180">
  41.  
  42. <p>Click the light bulb to turn on/off the light.</p>
  43.  
  44. <script>
  45. function changeImage(image) {
  46.     document.getElementById('myImage2').src = "pic_bulboff.gif";
  47.     if (image.src.match("bulbon"))
  48.         image.src = "pic_bulboff.gif";
  49.     else
  50.         image.src = "pic_bulbon.gif";
  51.    
  52. }
  53.  
  54. function changeImage2(image) {
  55.     document.getElementById('myImage').src = "pic_bulboff.gif";
  56.     if (image.src.match("bulbon"))
  57.         image.src = "pic_bulboff.gif";
  58.     else
  59.         image.src = "pic_bulbon.gif";
  60.    
  61. }
  62. </script>
  63.  
  64. </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement