Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototype: Change attribute

How can I change a <img> with the JS-Library Prototype? I managed it to get the element but I couldn't change the "src":

$('item1').getElementsBySelector('img')
like image 445
user199337 Avatar asked Dec 10 '22 18:12

user199337


2 Answers

var imgs = $('item1').getElementsBySelector('img');
imgs.each(function(img) {
    img.src = 'newSrc';
});
like image 96
David Hedlund Avatar answered Dec 30 '22 16:12

David Hedlund


You can also use the setAttribute function.

var imgs = $('item1').getElementsBySelector('img');
imgs.each(function(img) {
    img.setAttribute('src','newSrc');
    img.setAttribute('width','100px');
});
like image 31
aberpaul Avatar answered Dec 30 '22 17:12

aberpaul