Doubt in Javascript

Asked

Viewed 38 times

0

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

  • None. This will be ignored. O JS will only use the value if it is a valid attribute.

  • I don’t understand what you mean, how is that valid? because this generated attribute "dynamically" appears to be valid kkk, and you can invoke it after that.

  • What do you mean anything?

  • anything, you can type any attribute for a selected element

  • @Jefterrocha as @Maíonferreira said, attribute and property are different. "Is the attribute Anything accepted normally?" no. The attribute will be ignored. It is different to inform title, style etc that SANE valid attributes that will be embedded AS ATTRIBUTES in HTML. And this property will only be used, will only be useful if you create a specific code to use it, otherwise it will be ignored by JS because (repeat) are a attribute valid.

1 answer

0

Your question is a little crooked but I think I understand, correct me if I’m wrong.

You are 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.

Are different things!

Adding property to object(does not write to 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"));

Browser other questions tagged

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