Reputation: 16285
I have an <li>
tag and I want to have some info associated with it so that a piece of JavaScript can access it i.e. <li id='someVal' class='someVal' meta='additionalInfo'>
I would like to access the meta information through jQuery as follows $('#someVal').attr('meta')
is there a attribute like this?
Upvotes: 2
Views: 1490
Reputation: 72729
It is possible to add data to an element through jQuery:
$(element).data('name', 'value');
The data can be retrieved using:
$(element).data('name');
Or are you not trying to add data through jQuery?
In HTML5 there is also the possibility of custom data as I found on the web: http://ejohn.org/blog/html-5-data-attributes/
However as HTML5 is still a draft no guarantees there.
Upvotes: 6
Reputation: 15579
I suggest you use jQuery's attr()
method
$(<selector>).attr('name','value');
and retrieve with
$(<selector>).attr('name');
You can add any name to the DOM element. I am generally a fan of using such meta attributes if it makes sense to add it to the DOM element.
Say your app shows the numnber of schools in it. I will probably have a div with an attribute school
and set the id of the school as the value.
I know it is very similar to data but my rule of thumb use "attr" to extract metadata that's been added to the markup and use "data" to get/set dynamic application state.
Upvotes: 3