views:

148

answers:

3

Hello,

I have a function in a JavaScript like so:

$("#table-filters li").click(function(){ *** Lots of stuff here...
});

What I would like to happen is when the page loads, to one-time, run that function. Something contained in:

$(document).ready(function() { 
});

Suggestions?

Thanks!

+1  A: 

Use this:

$("#table-filters li").trigger("click");
Josh
That doesn't work. It appears that clicks ALL the LI's in the ul. Is there a way to just trigger a SINGLE click for the FIRST LI?
AnApprentice
ok got it! $("#table-filters li:first").trigger("click");Thanks for the tip
AnApprentice
+3  A: 
$(function(){ 
  $("#table-filters li")
    .click(function(){
      // Lots of stuff here...
    })
    .slice(0,1).click();
});
svinto
+1 For chaining.
Jonathan Sampson
+1 For chaining, and a creative use of `slice`. Though this is very clear, I just want to note that `.click()` without any parameters also triggers the click event.
Doug Neiner
@Dough, aaah, yeah I forgot about click(), probably because when triggering directly I usually bind to some custom event as well as click and trigger that custom event instead of click, so that other stuff bound to click won't trigger. But less is less and we like less, so I changed it.
svinto
@svinto Oh yeah, I do that to. The custom namespaces are killer!
Doug Neiner
A: 

$("#table-filters li:first").trigger("click");

AnApprentice
Your question didn't say you wanted only to click the first `li`. And two other users have already provided excellent answers. Accept one of theirs please.
Jonathan Sampson