+ 3
Adding attributes to an element using JavaScript
Is it possible to add an attribute to an element using JavaScript? If it is possible, how do I do it?
4 Respostas
+ 4
yes, for example you want to change the height attribute of an image..simply.. :
document.getElementById("img_id").setAttribute("height","50px");
+ 4
so if I want to add 2 attributes, I just need to add the .setAttribute twice?
+ 3
You may try this...
var img= document.getElementById("img_id");
img.setAttribute("height","50px");
img.setAttribute("width","50px");
+ 2
Yes
Or
You could also use a jquery command
$('#elementId').attr('attributename','newvalue');
You just have to add a different attributename for every attribute you wanna change