0

How can I use trigger() to trigger the click event on a particular link only?

for instance, I have this menu,

<li><a href="#" class="load-popup-public 500x400">1</a></li>
<li><a href="#" class="load-popup-public 500x400">2</a></li>
<li><a href="#" class="load-popup-public 500x400">3</a></li>
<li><a href="#" class="load-popup-public 500x400">4</a></li>

and I want to trigger the first link only when the page is loaded. so that I can call this function automatically when the page is loaded,

$('.load-popup-public').click(function (){
...
});
Run
  • 54,938
  • 169
  • 450
  • 748

2 Answers2

1

You can use :first selector to select the first link and then use document ready event.

Try this:

$(function(){
  $('.load-popup-public').click(function (){
   //Some code here
  });
 $(".load-popup-public:first").trigger("click");
})

Working example: http://jsfiddle.net/zeyWN/

Chandu
  • 81,493
  • 19
  • 133
  • 134
  • thanks so much! this is the answer I am looking for! not `ready`! thanks :-) – Run Jul 02 '11 at 01:48
  • @lauthiamkok, this DOES use the .ready function. $(function(){}) is the same as $(document).ready(){} – James Hill Jul 02 '11 at 01:50
  • @James: The question is about how to get hold of first anchor element in the collection not how to use ready event? – Chandu Jul 02 '11 at 01:51
  • @Cybernate, there's no question that your answer was the best (I neglected to add the :first selector), I just wanted+ the OP to understand that he did in fact use .ready to execute this when the page loads (one of his initial requests). +1 – James Hill Jul 02 '11 at 01:52
0

You can include this function in the .ready of jquery:

http://api.jquery.com/ready/

Julien Ducro
  • 854
  • 1
  • 9
  • 26