I am currently using jquery function(below) to display a block on click of a text.
can’t i generalize it $(‘#totalcomments’+id).click or something , In below code no is a integer. basically i have a set of repeating div’s whose format is hiddencomments1, hiddencomments2 ……… So on click on a text whose id=totalcomments1 i want to display the div whose id=hiddencomments1
function showcomments(no)
{
$('#totalcomments'+no).hide();
$('#hiddencomments'+no).show();
}
Looks like I’ve joined the show a little late, but you should really, really, really consider using
delegateorliveto bind your events instead ofclick.If, as you said in a comment, you could have up to 9,999 elements to bind this event for, with
clickyou’re getting each one and binding the same event to each of them; not good. Withliveordelegatehowever, you bind the event once to an shared-ancestor of the elements (documentin the case oflive), and take advantage of JavaScript’s event bubbling mechanism. This is infinitely more efficient.As mentioned by @lonesomeday in the comments, the difference between
liveanddelegateis the syntax you use to first bind the event;liveselects the elements to begin with, where-asdelegatedoesn’t.(
delegate>live>click)Whether it’s a
id-starts-withorclassapproach you choose, the notion is still the same:or
Again, looking at this from a performance point of view, using a class based system would be quicker.
I’ve done one example using
liveand another withdelegate, to provide an example of using both.