Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepopulate jQuery Data in html

Tags:

html

jquery

jQuery has the very cool feature/method ".data", I wonder if there is a way to have the data in the code so that jQuery can use it when the rendering of html is done. Suppose i have a repeater and looping out children, and I want to add some data to those children without using classes etc. Will I have to add javascript to that repeater just to add stuff to the "data of jquery" or is there some better way?

like image 926
Mikael Gidmark Avatar asked Oct 14 '22 06:10

Mikael Gidmark


2 Answers

There is the metadata plugin which might do what you are talking about

For example, you can do: (You can pick from different format by setting an option)

<li class="someclass {some: 'data'} anotherclass">...</li>
OR
<li data="{some:'random', json: 'data'}">...</li>
OR
<li><script type="data">{some:"json",data:true}</script> ...</li>
OR
<li data-some="'random'" data-json="'data'">...</li>

After that you can simply do:

var data = $('li.someclass').metadata();
if ( data.some && data.some == 'data' )
    alert('It Worked!');
like image 162
PetersenDidIt Avatar answered Oct 18 '22 08:10

PetersenDidIt


HTML 5 has a new standard for attributes starting with "data-". See here link text.

You could use this to store data and use a selector to parse out the data.

like image 36
James Westgate Avatar answered Oct 18 '22 09:10

James Westgate