What is the difference between setAttribute () and setAttributeNode ()?

1

Could anyone get that doubt? I need to know the difference between setAttribute () and setAttributeNode ()?

Thank you!

    
asked by anonymous 06.09.2017 / 00:46

1 answer

1

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
    
06.09.2017 / 01:16