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>
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.
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'); });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With