JQuery trigger click on first child

I am trying to trigger a click on the first child .loader when the document is ready, but without success:

HTML:

 <a class="loader" data-target="david-boy" title="Danny Boy-Rivera"> <span class="img"><span class="play"></span><img src="img/gallery-image1.jpg" alt="" /></span> <h2>Danny Boy</h2> </a> 

JQuery

 //Does not work $('a.loader:first-child').bind('click'); $('a.loader:first-child').trigger('click'); $('a.loader:first-child').click(); //Works $('a.loader:first-child').css("background", "red"); 

Any ideas why?

Update

Handler:

 $('.loader').click(function(){ var name=$(this).data("target"); callVideo(name); }); 

Update2

So the problem was that I declared the :first-child action before the handler. I changed their places and everything is fine

+4
source share
2 answers

Did you define the handler to which you initiated the initiated 'click'?

Here EXACTLY the same code from your violin, except that I placed the handler BEFORE the trigger

 $(document).ready(function(){ $('.loader').click(function(){ alert($(this).data("target")); }); $('.loader:first-child').click(); $('.loader:first-child').css("background", "red"); });​ 

The warning you are looking for appears

+6
source

You need to have a click event handler ...

 $('a.loader:first-child').click(function(){ alert('clicked'); }); $('a.loader:first-child').click(); 

Update:

Your handler probably was attached before the item was ready.

 $(function(){ $('.loader').click(function(){ var name=$(this).data("target"); callVideo(name); }); }); 
+5
source

Source: https://habr.com/ru/post/1411804/


All Articles