Nuno_147
Nuno_147

Reputation: 2913

Using pre-made elements in html tags

I would like to understand if using pre-made elements withn an HTML tag consider to be valid. I have start to use it all over my HTML to make my code become more generic and less breakable. However I am unaware if this is valid and supported by all browsers.

For example , I am doing something like that :

<a class="item-class" href="...' itemname="something">

now assume I am doing an jquery onclick event for all item-class I can do

$(this).attr("itemname")

kind of passing parameters to jquery events.

Upvotes: 1

Views: 120

Answers (2)

Dunhamzzz
Dunhamzzz

Reputation: 14798

What you should be using for this sort of meta data, as per the HTML5 spec is the data-* attribute. It works basically the same, it's just you use `data-itemname="itemname" instead.

The is also a proper jQuery function for editing/retrieving these values:

<a class="item-class" href="...' data-itemname="something">
$(this).data("itemname")

Upvotes: 1

Ram
Ram

Reputation: 144689

itemanem is not a valid attribute you can use HTML5 data-* attribute and jQuery data() method instead:

<a class="item-class" href="...' data-name="something">

$(this).data("name")

However, if you want to read the non-standard attributes you can use getAttribute method:

var itemName = $(this)[0].getAttribute("itemname")

Upvotes: 0

Related Questions