+ 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?

11th Dec 2017, 12:35 PM
luʁi
4 Respuestas
+ 4
yes, for example you want to change the height attribute of an image..simply.. : document.getElementById("img_id").setAttribute("height","50px");
11th Dec 2017, 12:56 PM
Md. Nafis Ul Haque Shifat
Md. Nafis Ul Haque Shifat - avatar
+ 4
so if I want to add 2 attributes, I just need to add the .setAttribute twice?
11th Dec 2017, 1:45 PM
luʁi
+ 3
You may try this... var img= document.getElementById("img_id"); img.setAttribute("height","50px"); img.setAttribute("width","50px");
11th Dec 2017, 3:15 PM
Md. Nafis Ul Haque Shifat
Md. Nafis Ul Haque Shifat - avatar
+ 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
24th Dec 2017, 11:30 AM
the_code_genin
the_code_genin - avatar