How to change an img src with javascript?

cosmichero2025

I know there are other questions like this and I've tried following them I'm just not aware of what exactly I'm doing wrong. I've declared the pic variable as being linked to the image with the corresponding id of 'pic' and I've tried many different examples and trying to follow other questions like this but to no avail.

--- THE REAL QUESTION ----

I would like the image to change its src to another one that I have in my workspace with the click of a button.

HTML:

<img class="trans" id="pic" src="images/link_rouge.png" alt="" width="1000" height="333" /> 

JavaScript:

var pic = document.getElementById('pic');

function rouge() {
    pic.src = "images/link_rouge.png";
}

function blue() {
    pic.src = "images/link_blue.png";
}

I know the functions already work with the buttons because they are affecting some divs on the page that change color the only things not changing are the images.

Rayon

The EventTarget.addEventListener() method registers the specified listener on the EventTarget it's called on.

Use addEventListener over button elements to attach click events and bind your handler functions to those events.

var pic = document.getElementById('pic');

function rouge() {
  pic.src = "http://www.projectvictorycosplay.com/images/zelda/Links/3198_render_link.png";
}

function blue() {
  pic.src = "http://bin.smwcentral.net/u/1944/Link%2BBlue%2BTP%2Bshrunk.png";
}
document.getElementById('btn1').addEventListener('click', rouge);
document.getElementById('btn2').addEventListener('click', blue);
img {
  width: 200px;
}
<button id='btn1'>rouge</button>
<button id='btn2'>blue</button>
<br/>
<img class="trans" id="pic" src="http://www.projectvictorycosplay.com/images/zelda/Links/3198_render_link.png" alt="" width="1000" height="333" />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaScript Change img src automatically

From Dev

Javascript change img src not working

From Dev

Javascript change img src on load

From Dev

Unable to change the src of an img element using javascript

From Dev

Check JSON for img tag and change src in JavaScript

From Dev

jQuery how to change img src path onclick?

From Dev

How to change the level of img src attribute

From Dev

lxml -- how to change img src to absolute link

From Dev

How to change img src in view MVC

From Dev

How to change img src added by on click function?

From Dev

how to change the src of an< img> when I know the <img> id?

From Dev

How to use JavaScript variable in img src attribute?

From Dev

Change img src in response to media query (CSS or Javascript)

From Dev

Javascript img src variation?

From Dev

change img src click listener to change second img src

From Dev

Change href and src of an img onclick

From Java

Programmatically change the src of an img tag

From Dev

Use regex to change img src

From Dev

change img src on img click angularjs

From Dev

How Can Inline IMG SRC data:image Be Transferred With JavaScript to PHP?

From Dev

How Can Inline IMG SRC data:image Be Transferred With JavaScript to PHP?

From Dev

How set photos to an img src from Flickr api in javascript

From Dev

How to grab the src of img tag that is within an anchor tag with javascript?

From Dev

How to change source of img using javascript

From Dev

how to refresh the img src when src is change while using magnifier js?

From Dev

how to refresh the img src when src is change while using magnifier js?

From Dev

how to change src fo image element by javascript

From Dev

how to change src value using javascript

From Dev

How to change image src in a div with javascript?

Related Related

  1. 1

    JavaScript Change img src automatically

  2. 2

    Javascript change img src not working

  3. 3

    Javascript change img src on load

  4. 4

    Unable to change the src of an img element using javascript

  5. 5

    Check JSON for img tag and change src in JavaScript

  6. 6

    jQuery how to change img src path onclick?

  7. 7

    How to change the level of img src attribute

  8. 8

    lxml -- how to change img src to absolute link

  9. 9

    How to change img src in view MVC

  10. 10

    How to change img src added by on click function?

  11. 11

    how to change the src of an< img> when I know the <img> id?

  12. 12

    How to use JavaScript variable in img src attribute?

  13. 13

    Change img src in response to media query (CSS or Javascript)

  14. 14

    Javascript img src variation?

  15. 15

    change img src click listener to change second img src

  16. 16

    Change href and src of an img onclick

  17. 17

    Programmatically change the src of an img tag

  18. 18

    Use regex to change img src

  19. 19

    change img src on img click angularjs

  20. 20

    How Can Inline IMG SRC data:image Be Transferred With JavaScript to PHP?

  21. 21

    How Can Inline IMG SRC data:image Be Transferred With JavaScript to PHP?

  22. 22

    How set photos to an img src from Flickr api in javascript

  23. 23

    How to grab the src of img tag that is within an anchor tag with javascript?

  24. 24

    How to change source of img using javascript

  25. 25

    how to refresh the img src when src is change while using magnifier js?

  26. 26

    how to refresh the img src when src is change while using magnifier js?

  27. 27

    how to change src fo image element by javascript

  28. 28

    how to change src value using javascript

  29. 29

    How to change image src in a div with javascript?

HotTag

Archive