Run
Run

Reputation: 57196

jquery trigger: how can I use it to trigger a link when the page is loaded

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 (){
...
});

Upvotes: 0

Views: 1316

Answers (2)

Chandu
Chandu

Reputation: 82913

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/

Upvotes: 1

Julien Ducro
Julien Ducro

Reputation: 854

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

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

Upvotes: 0

Related Questions