jQuery trigger click on first child

click, jquery, triggers

Solution

Did you define the handler before you initiated the triggered 'click'?

Here's the EXACT same code from your fiddle except I put 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");
});​

This will pop the alert you're looking for

Problem

I'm trying to invoke a click on first `.loader` child when document is ready, but no 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 had declared the `:first-child` action before the handler. I changed their places and everything ok

Original source