[javascript] jQuery or JavaScript auto click

How can I auto click on the link on page load? I have been trying for ages but id does not work.

<link rel="stylesheet" type="text/css" href="leightbox.css" />
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="leightbox.js"></script>
</head>

<body>


<div class="content">
<p> <a href="#" class="lbOn" rel="pop02">Click here to activate leightbox popup.</a></p>
</div>



<!----------// POPUP (ON CLICK) //---------->
<div id="pop02" class="leightbox">
<a href="#" class="lbAction" rel="deactivate">×</a>

<div class="scrollbox">
<h1>This popup loads upon clicking a trigger link.</h1>
text</div>
</div>

</body>

This question is related to javascript jquery

The answer is


$(document).ready(function(){ 
  $('#some-id').trigger('click'); 
});

did the trick.


In jQuery you can trigger a click like this:

$('#foo').trigger('click');

More here:

http://api.jquery.com/trigger/

If you want to do the same using prototype, it looks like this:

$('foo').simulate('click');

$(document).ready(function(){
   $('.lbOn').click();
});

Suppose this would work too.


You are trying to make a popup work maybe? I don't know how to emulate click, maybe you can try to fire click event somehow, but I don't know if it is possible. More than likely such functionality is not implemented, because of security and privacy concerns.

You can use div with position:absolute to emulate popup at the same page. If you insist creating another page, I cannot help you. Maybe somebody else with more experience will add his 15 cents.


First i tried with this sample code:

$(document).ready(function(){ 
     $('#upload-file').click(); 
});

It didn't work for me. Then after, tried with this

$(document).ready(function(){ 
     $('#upload-file')[0].click(); 
});

No change. At last, tried with this

$(document).ready(function(){ 
     $('#upload-file')[0].click(function(){
     }); 
});

Solved my problem. Helpful for anyone.