Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery or JavaScript auto click

Tags:

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> 
like image 392
eMRe Avatar asked Nov 08 '10 11:11

eMRe


People also ask

How to onclick in jQuery?

To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.


2 Answers

You haven't provided your javascript code, but the usual cause of this type of issue is not waiting till the page is loaded. Remember that most javascript is executed before the DOM is loaded, so code trying to manipulate it won't work.

To run code after the page has finished loading, use the $(document).ready callback:

$(document).ready(function(){   $('#some-id').trigger('click'); }); 
like image 152
Zack Bloom Avatar answered Sep 18 '22 13:09

Zack Bloom


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.

like image 24
Edukondalu Thaviti Avatar answered Sep 22 '22 13:09

Edukondalu Thaviti