5

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

1
  • I do have a handler, check update Commented May 10, 2012 at 14:41

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

6 Comments

Where did you see his fiddle? I couldn't... :)
I guess he was referring to the one sent by Bondye
Maybe, there was a fiddle...sometimes gets jumbled, in any case defining the handler after resulted in nothing happenening, moving it before made it work
Yet again I shouldn't have assumed that it would have been logical to declare something before calling it, right? :)
I personally like to be told what it is I have to do before being told to do it...I think anyone with a bad boss can sympathize with this function though.
|
5

You need to have a handler to the click event...

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

$('a.loader:first-child').click();

Update:

You're handler was probably attached before the element was ready.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.