0

document.querySelector('#element')['something'] = 'whatever' What am I achieving by setting some sort of invisible attribute on the element? Is there any documentation on this?

I played around with it, confirmed that I can retrieve the value later and now I'm wondering is how useful it is and how reliable it is.

Mr.Kleiner
  • 15
  • 1
  • 5
  • I think that this is not an attribute, you just add property to object which you selected – EzioMercer Apr 01 '22 at 15:15
  • This is called [bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#examples) and it can be very useful. However, you wouldn't normally use it to create custom properties on an html element as in your example. – Yogi Apr 01 '22 at 15:27

1 Answers1

0

Yes, it can be done. But it's not advised. Remember that html and dom have many standards to adhere to and with changes such as these, you are testing the limits of user's browsers expectations.

This is more clearly explained in the answer to this question: Javascript DOM: Setting custom DOM element properties

LeftBrain
  • 326
  • 2
  • 10
  • Thank you, it makes sense now. I'm not dealing with user browsers, I'm writing a simple electron application. – Mr.Kleiner Apr 01 '22 at 15:24