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

<img src="https://stackoverflow.com/questions/11722400/template/edit.png" name=edit-save/>

at first I have a default src which is “https://stackoverflow.com/questions/11722400/template/edit.png” and I wanted to change it with “https://stackoverflow.com/questions/11722400/template/save.png” onclick.

UPDATED:
here’s my html onclick:

<a href="#" onclick='edit()'><img src="https://stackoverflow.com/questions/11722400/template/edit.png" id="edit-save"/></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 = "https://stackoverflow.com/questions/11722400/template/save.png";
    }

9 Answers
9

Leave a Reply

Your email address will not be published. Required fields are marked *