Learning Learning - 23 days ago 5
Javascript Question

jQuery trigger click event not working on div

I want to trigger a click event on a

div
. I use the following code but it is not working.

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


<div id="showbanner">show banner</div>


What is that I am doing wrong? I actually want to show the banner 2 seconds after page is loaded. Before I could add delay event my click event for this div is not firing.

Here is my code example: http://codepen.io/anon/pen/aOzyxo

Answer

The issue is because your trigger('click'); call is the first thing in your script; it's called before the event is attached.

You need to move it to the end of the script so that the event handlers are bound when it is called:

Updated Codepen

Comments