Hi All,
I am using jQuery and I am loading dynamic HTML via PHP. My question is, what is the best practice concerning elements/attributes that may or may not exist on any given page? Should I have one .js file containing all the possibilities (I would think this is inefficient) or should I use a bunch of script tags (that seems also inefficient)? Is there a "lazy" initialization way to do this?
For instance, say I have the following:
<div id="list">
<ul>
<li>some</li>
<li>stuff</li>
<li>here</li>
</ul>
</div>
But this list is generated only if necessary, not on all pages. Should my global js file have the following:
$("#list").click( function(){
$(this).removeClass('collapse').addClass('expand');
});
Or should I add it in a script tag just above the div that is being generated at the bottom of the page after creating the div is output via PHP?
Thanks SO, you guys are awesome. Can't wait until I know enough to start answering some questions!
Edit, just thought of a quick way to ask. Which is better, jQuery running with a bunch of selectors that don't match or passing javascript in script tags via PHP?