Doubt in JavaScript

0

Good evening and sorry for the little objective title but I found it difficult to title this issue but let's go! In an example element selector document.querySelector().anything is the anything attribute normally accepted? I can type anything in the place, potato, lobster and it will be accepted, if I do document.querySelector("#anyID").batata = "doce". it works normally. It seems to me that the language automatically creates the potato index and assigns the string "sweet" to it ... what's the use of this in terms of programming?

    
asked by anonymous 04.02.2018 / 00:06

1 answer

0

Your question is kind of crooked but I think I understood, correct me if I'm misinterpreted.

Are you asking if adding an attribute to an element using DOM is common (valid, accepted, normal, etc.)?

Well, first you're not adding attribute, but property to the object.

It's different things!

Adding property to the object (do not write in HTML):

var input = document.querySelector('input');
input.data = "value";
console.log(input.data);

Adding attribute to object (writes to HTML and DOM):

var input = document.querySelector('input');
input.setAttribute('data', 'value');
console.log(input.getAttribute("data"));
    
04.02.2018 / 05:05