jQuery: How to store data for non-existent elements? -


what approach of storing data dom elements not exist yet, when created obtain data.

i'm going make assumptions here since question little vague @ moment, sounds want manipulate dom elements not yet exist, because loaded ajaxily.

jquery provides function called .live() lets bind event handler elements exist or in future.

so, if wanted add data attribute of <p> elements--even not yet exist in dom--you this:

// hook event handler runs anytime <p> inserted dom $('p').live('domnodeinserted', function() {     $(this).attr('title', 'foo'); });  // insert couple <p> elements , see title attributes update $('body').append('<p>hello</p>'); $('body').append('<p>world</p>'); 

working example: http://jsfiddle.net/rsobers/tlxeu/3/

edit: instead of using attributes store data, can make use of jquery's .data() function instead. better place custom data.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -