Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom HTML attributes and jQuery

Tags:

html

jquery

I am often needing to store little pieces of reference in a page element, most of the time a <div>. I couldn't find too much documentation on how valid it would be to create my own attributes. What do you suggest is the most valid way of doing this or can I even do this? If so, can I just use jQuery's attr() call to get the reference?

example : <div class="sample" dataref="Sample Data Reference"></div>

like image 860
Tim Joyce Avatar asked Sep 28 '11 11:09

Tim Joyce


1 Answers

Use HTML5 data attributes

See: http://api.jquery.com/data/

E.g.

<div class="sample" data-ref="some string" />

Then:

$('.sample').data('ref');
like image 167
Petah Avatar answered Nov 15 '22 20:11

Petah