Changing the src of an image with javascript

javascript set image src dynamically
javascript change image onclick event
change image on button click javascript
img src javascript function
change src of image jquery
get image src javascript
how to insert image in javascript code
javascript display images dynamically

I have been trying to use the following piece of code to change the src of an image

document.getElementById('test').src='images/test2.png';

My code looks like this. There's some CSS and other things in there but the important parts are the div and the img, and the function.

<div id='splashScreen' class='splash'>
  <button class='bigButton' onclick='introSplash()'>Hi</button>
  <img class='splashImages' src='images/splashScreen1.png'>
</div>

this is my image with a page-sized transparent button over the top of it

<script>
function introSplash() {
    document.getElementById('splashImages').src='images/splashScreen2.png';
  }
</script>

this function should be replacing the image under the button.

Pressing the button with this code returns the following error message:

Uncaught TypeError: Cannot set property 'src' of null at introSplash

all of the images are locally saved and exist with those names

I'm not sure why this is occurring, and would appreciate any help.


You need to add the ID test to the <img> element:

<img id='test' class='splashImages' src='images/splashScreen1.png' />

Now your code will work.

The alternative, without adding any IDs, would be to use document.getElementsByClassName like so:

document.getElementsByClassName('splashImages')[0].src='images/test2.png';

JavaScript can change HTML attributes, <p>JavaScript can change HTML attribute values.</p> ​ <p>In this case JavaScript changes the value of the src (source) attribute of an image.</p> ​ <​button  The src attribute specifies the URL of an image. Therefore, by setting a new src, we can dynamically change the image in question. In this post, I will show you how to accomplish this using both regular JavaScript and jQuery. Changing the src attribute using regular JavaScript.


Please verify you are using document.getElementById('splashImages') but the html element has not ID. Add the id attribute

<img id="splashImages" class='splashImages' src='images/splashScreen1.png'>

Please try this and let me know how it works :)

Programmatically change the src of an img tag, You can use both jquery and javascript method: if you have two images for example: <img class="image1" src="image1.jpg" alt="image"> <img  You've got a few changes (this assumes you indeed still want to change the image with an ID of IMG, if not use Shadow Wizard's solution). Remove a.src and replace with a: <script type="text/javascript"> function changeImage(a) { document.getElementById("img").src=a; } </script>


You try to get the element by id, but only a class="splashImages" is given.

How to change the src attribute of an img element in JavaScript , Example 1: This example changes the src attribute of image by using JavaScript. filter_none. edit close. play_arrow. link brightness_4 code  The task is to change the src attribute of the <img> element by using JQuery and JavaScript. jQuery prop () Method: This method set/return properties and values of the matched elements. If this method is used to return the property value, it returns the value of the first selected element.


document.getElementById('splashImages') returns null because the element you want does not have the correct id. Use getElementsByClassName instead, or give it the correct id.

So you should implement one of following changes:

<script>
function introSplash() {
    document.getElementsByClassName('splashImages').src='images/splashScreen2.png';
  }
</script>

or

<div id='splashScreen' class='splash'>
  <button class='bigButton' onclick='introSplash()'>Hi</button>
  <img id='splashImages' class='splashImages' src='images/splashScreen1.png'>
</div>

Way to set src attribute and alt using JavaScript., You can assign and ID to your image and then change your SRC and ALT using JavaScript: document.getElementById("imageid").src=". In the above code snippet, getElementById finds a image with id “ myImage ” and changes the image by each click on "Change" button. In the <script> code image.src.match() attribute is used in this .match() tests if image.src contains the string "colorbottel". If there is match, .match() changes image.src to "waterbottel". OUTPUT. Onclick of "Change" button in the above output wil change the water bottlel into Softdrink bottle, that can be shown in the below image.


Using JavaScript to Change an Image SRC Attribute, Changing the src attribute of an image is pretty easy to do with a bit of Javascript. In this hands-on tutorial you'll learn how to do it. Code included! < p > In this case JavaScript changes the value of the src (source) attribute of an image. </ p > < button onclick = "document.getElementById('myImage').src='pic_bulbon.gif'" > Turn on the light </ button >


How to Change the Image Source Using jQuery, You can use the attr() method to change the image source (i.e. the src attribute of the <img> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> Before clicking on the button: After clicking on the button: Approach 2: Create an empty image instance using new Image().; Then set its attributes like (src, height, width, alt, title etc).


Change the "src" attribute of an image using JavaScript., Therefore, by setting a new src, we can dynamically change the image in question. In this post, I will show you how to accomplish this using both regular JavaScript  The HTML code of the original image tag we are changing is: <img id="image" src="/images/sony-x10-headphones.png"> So when we use the Javascript line document.getElementById ("image") we are telling Javascript to get HTML element which has an id of image. The attachment.src. is referencing the image's src attribute.