Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
106
rated 0 times [  113] [ 7]  / answers: 1 / hits: 86401  / 12 Years ago, fri, march 16, 2012, 12:00:00

How to change the src attribute of a HTMLImageElement in JavaScript?



I need help to convert logo.attr('src','img/rm2.png') to vanilla JavaScript.



window.onresize = window.onload = function () {
if (window.innerWidth > 1536) {
var logo = document.getElementById('rm');
logo.attr('src','img/rm2.png');
}
};

More From » image

 Answers
54

You mean you want to use pure javascript?



This should do it:



var logo = document.getElementById('rm');
logo.src = img/rm2.png;


So your function should look like :



window.onresize = window.onload = function () {
if (window.innerWidth > 1536) {
var logo = document.getElementById('rm');
logo.src = img/rm2.png;
}
};


Note: You could also use element.setAttribute. BUT, see this post for more:

When to use setAttribute vs .attribute= in JavaScript?


[#86810] Thursday, March 15, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andreasn

Total Points: 238
Total Questions: 107
Total Answers: 111

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
;