322

How can I change the src attribute of an img tag using javascript?

<img src="../template/edit.png" name="edit-save" alt="Edit" />

at first I have a default src which is "../template/edit.png" and I wanted to change it with "../template/save.png" onclick.

UPDATED: here's my html onclick:

<a href="#" onclick="edit()"><img src="../template/edit.png" id="edit-save" alt="Edit" /></a>

and my JS

function edit()
{   
    var inputs = document.myform;
    for(var i = 0; i < inputs.length; i++) {
        inputs[i].disabled = false;
    }
}

I've tried inserting this inside the edit(), it works but need to click the image twice

var edit_save = document.getElementById("edit-save");
    edit_save.onclick = function(){
       this.src = "../template/save.png";
    }
Andy
  • 4,783
  • 2
  • 26
  • 51
Sam San
  • 6,567
  • 8
  • 33
  • 51

9 Answers9

412

Give your img tag an id, then you can

document.getElementById("imageid").src="../template/save.png";
Matthias
  • 12,704
  • 13
  • 35
  • 56
  • 21
    For simple things like setting the value of an input or changing an image src (**especial** when you're using elements that have IDs), you really should try to avoid jQuery, since the call is so much slower than the pure JS call – Brian Leishman Apr 08 '15 at 13:36
  • 2
    @william44isme and our page will be loaded slower than before. i think the jQuery is useful just for websites that uses a same code, very more. for example if we use the above code more than 30 or 50 times. so it's necessary to use jQuery – Mahdi Jazini Nov 30 '16 at 17:44
82

You can use both jquery and javascript method: if you have two images for example:

<img class="image1" src="image1.jpg" alt="image">
<img class="image2" src="image2.jpg" alt="image">

1)Jquery Method->

$(".image2").attr("src","image1.jpg");

2)Javascript Method->

var image = document.getElementsByClassName("image2");
image.src = "image1.jpg"

For this type of issue jquery is the simple one to use.

chandanjha
  • 891
  • 6
  • 5
  • 40
    The poster never indicated any interest in a jQuery solution. Nor is the jQuery method simpler. Do not answer a JS questions with jQuery solutions (unless the post indicates they want that). It just confuses people trying to learn JS. – metaColin Jan 30 '17 at 17:54
  • 12
    getElementByClassName returns collection of all elements. We need to mention index of the element too. var image = document.getElementsByClassName("image2")[0]; image.src = "image1.jpg" – Tushar Gaurav Jan 07 '18 at 17:44
39

if you use the JQuery library use this instruction:

$("#imageID").attr('src', 'srcImage.jpg');
Foreever
  • 7,099
  • 8
  • 53
  • 55
Alessandro Pirovano
  • 2,509
  • 28
  • 21
  • 31
    The question does not ask for a jQuery answer, include a jQuery tag, or suggest that it's okay to use jQuery. – Popnoodles Sep 03 '15 at 11:41
  • 12
    Telling folks to use jQuery when an equivalent function is built right into Javascript makes no sense. Every JS question does not require a jQuery answer. This solution is a kludge that prevents people from learning about what vanilla JS can already do. – metaColin Jan 30 '17 at 17:37
33

its ok now

function edit()
{   
    var inputs = document.myform;
    for(var i = 0; i < inputs.length; i++) {
        inputs[i].disabled = false;
    }

    var edit_save = document.getElementById("edit-save");

       edit_save.src = "../template/save.png";                              
}
Sam San
  • 6,567
  • 8
  • 33
  • 51
14

Give your image an id. Then you can do this in your javascript.

document.getElementById("blaah").src="blaah";

You can use this syntax to change the value of any attribute of any element.

Ben
  • 2,200
  • 20
  • 30
9
<img src="../template/edit.png" name="edit-save" onclick="this.src = '../template/save.png'" />
Donatas Olsevičius
  • 1,350
  • 8
  • 13
8

With the snippet you provided (and without making assumptions about the parents of the element) you could get a reference to the image with

document.querySelector('img[name="edit-save"]');

and change the src with

document.querySelector('img[name="edit-save"]').src = "..."

so you could achieve the desired effect with

var img = document.querySelector('img[name="edit-save"]');
img.onclick = function() {
    this.src = "..." // this is the reference to the image itself
};

otherwise, as other suggested, if you're in control of the code, it's better to assign an id to the image a get a reference with getElementById (since it's the fastest method to retrieve an element)

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
8

In this case, as you want to change the src of the first value of your element, you have no need to build up a function. You can change this right in the element:

<a href='#' onclick='this.firstChild.src="../template/save.png"')'>
  <img src="../template/edit.png" id="edit-save"/>
</a>

You have several ways to do this. You can also create a function to automatize the process:

function changeSrc(p, t) { /* where p: Parent, t: ToSource */
  p.firstChild.src = t
}

Then you can:

<a href='#' onclick='changeSrc(this, "../template/save.png");'>
  <img src="../template/edit.png" id="edit-save"/>
</a>
Marcelo Camargo
  • 2,240
  • 2
  • 22
  • 51
4

Maybe because you have a tag like a parent of the tag. That why you have to click two time the images.

For me the solution is this: http://www.w3schools.com/js/tryit.asp?filename=tryjs_intro_lightbulb

Po Po
  • 231
  • 3
  • 8