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

Asked

Viewed 193 times

1

Could someone take away this doubt? I need to know the difference between setAttribute() and setAttributeNode()?

Grateful!

1 answer

1


Both have the same function: create (or change) an attribute in the element and set a value. 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.