Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you define your own HTML element attributes?

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.

like image 807
Alex Avatar asked Aug 01 '11 14:08

Alex


People also ask

Can you define your own HTML tags?

That's completely doable. Just create a new element, then use the standard css styles to give it the look you want. Sample!

How do you define an element in HTML?

An HTML element is defined by a start tag, some content, and an end tag.

How are attributes defined in HTML?

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.


2 Answers

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.

like image 78
Quentin Avatar answered Sep 24 '22 06:09

Quentin


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");
like image 41
ShankarSangoli Avatar answered Sep 24 '22 06:09

ShankarSangoli