HTML Elements have Variables? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

HTML Elements have Variables?

Attributes in html has weird behaviours...but why can they be used as variables...and why has no one realised this before? https://code.sololearn.com/WUI6X1O8zCbG/?ref=app Is there a downside to using made up attributes as variables? I know one such may be confusing real attributes such as style, but anything else?

24th Jul 2018, 7:36 PM
Andre Daniel
Andre Daniel - avatar
5 Answers
+ 12
It's common to use attributes as variables. Usually starts with data- and used along with javascript https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
24th Jul 2018, 7:38 PM
Toni Isotalo
Toni Isotalo - avatar
+ 4
Data attributes are commonly use on event trigger on class elements. We should use the format data-<name> which is reserved for data attribute, in order to avoid conflict with standard attributes. https://code.sololearn.com/Wft4xKwN2waD/?ref=app
25th Jul 2018, 4:45 PM
Calviղ
Calviղ - avatar
+ 2
Toni Isotalo woah. I found out randomly today that it was a possibility.
24th Jul 2018, 8:02 PM
Andre Daniel
Andre Daniel - avatar
+ 2
But we don't necessarily need to use data though 😉
25th Jul 2018, 5:43 PM
Andre Daniel
Andre Daniel - avatar
0
No html elements does not have variables
4th Apr 2021, 4:54 PM
Mukesh.G
Mukesh.G - avatar