Could anyone get that doubt? I need to know the difference between setAttribute () and setAttributeNode ()?
Thank you!
Could anyone get that doubt? I need to know the difference between setAttribute () and setAttributeNode ()?
Thank you!
Both have the same function: create (or change) an attribute in the element and set a value. It only changes the form of construction (if the attribute already exists, it will be ALL replaced by the set value):
setAttribute:
document.getElementsByTagName("input")[0].setAttribute("name", "teste"); // crio/altero o atributo name="teste" no input
setAttributeNode:
var elemento = document.getElementsByTagName("input")[0];
var atributo = document.createAttribute("name");
atributo.value = "teste";
elemento.setAttributeNode(atributo); // crio/altero o atributo name="teste" no input