JavaScript Change image when clicking button change image when hover,change image when clicked,change image

This seems like it should work but doesn't. I'm not sure where the problem is - either I'm doing it wrong, or it's possible I have a syntax error. I just doesn't do anything. I'm trying to get the current picture to change when the button is clicked. I'm a beginner at Javascript, so please be gentle ;) Thank you!

<html>
<script>
function pictureChange()
{
document.getElementById(theImage).src="http://31.media.tumblr.com/fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png");
}
</script>
<body>
<img id="theImage" src="http://31.media.tumblr.com/18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwM01soh1p8o1_500.png">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange()"></p>
</body>
</html>
Answer:1

You missed the quotes in .getElementById('theImage')

 function pictureChange()
    {
    document.getElementById('theImage').src="http://31.media.tumblr.com/fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png";
    }
Answer:2

Add " to getElementById argument and remove ) at the end of the line:

<script>
    function pictureChange()
    {
          document.getElementById("theImage").src="http://31.media.tumblr.com/fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png";
    }
</script>

http://jsfiddle.net/cDd8J/ - here. It works.

theImage is just id of the element, not variable, so you have to put it in quotes.

Answer:3

There are lot of ways you could try.Calling the function using inline attributes or calling it using the id in your script.Here's one ,

theButton.onclick = function pictureChange()
{
   document.getElementById("theImage").src="http://31.media.tumblr.com/fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png";
}

Demo

Answer:4

Currently I have a Javascript function that uses I can hard code values in like - data: [1,4,7,9] However I wish to pass in an integer list to set the values from the code behind something like - C#...

Currently I have a Javascript function that uses I can hard code values in like - data: [1,4,7,9] However I wish to pass in an integer list to set the values from the code behind something like - C#...

I want to sort my table data in expandable table view, so I tried the below code: Code Link and my view is as in the below image When I use this code and put in my site, it is not working in this ...

I want to sort my table data in expandable table view, so I tried the below code: Code Link and my view is as in the below image When I use this code and put in my site, it is not working in this ...

With PhantomJS, I want to gather perfomance timing on page load time. I test this 2 methods for gathering metrics Setting own timing var page = require('webpage').create(); var system = ...

With PhantomJS, I want to gather perfomance timing on page load time. I test this 2 methods for gathering metrics Setting own timing var page = require('webpage').create(); var system = ...

I have a jqgrid and the first column I have a checkbox (checkAll others checkbox). I don't know how i do it. How I do a function to select all others checkbox in my jqgrid? $.getJSON("/Page/Table", ...

I have a jqgrid and the first column I have a checkbox (checkAll others checkbox). I don't know how i do it. How I do a function to select all others checkbox in my jqgrid? $.getJSON("/Page/Table", ...

  1. function select all checkbox
  2. select checkbox function