I was wondering if you can define your own attributes in HTML. For example in HTML divs there's a range of attributes, such as style
, title
, class
etc. Can you make up and add your own, while remaining syntactically correct?
For example: <div class="example" test_item="20"></div>
The need for this is with regards to Javascript and data.
That's completely doable. Just create a new element, then use the standard css styles to give it the look you want. Sample!
An HTML element is defined by a start tag, some content, and an end tag.
In HTML, an attribute is a characteristic of a page element, such as font size or color. Attributes are used to amplify a tag. When a Web browser interprets an HTML tag, it will also look for its attributes so that it can display the Web page's elements properly.
With one exception — no. HTML uses the attributes and elements defined by the specification, and only those attributes and elements.
That exception is attributes with names starting data-
, and then only if you are following the HTML 5 draft.
That said, it is often appropriate to encode the data in the id
or class
attribute.
You can define the data attribute for any element as follows, and use jQuery data method to retrieve those attributes easily.
<div class="example" data-mydata="mydata")></div>
//In jquery to retrieve mydata you have to just say
$(".example").data("mydata");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With