views:

39

answers:

2

Hello - I am having trouble getting this to work. I would like to trigger the second link. If anyone can help that would be much appreciated.

    $(".links").click(function () {
        alert($(this));
    })


    function someFunction(){
        $(".links").trigger('click');
    }

    someFunction();

    ...
    <a href="1.html" class="links">One</a>
    <a href="2.html" class="links">Two</a>
    <a href="3.html" class="links">Three</a>
+1  A: 

Have someFunction() accept an argument that is the 0 based index of link you want to click.

function someFunction( n ){
    $(".links:eq(" + n + ")").trigger('click');
}

someFunction( 1 ); // Pass 1 to trigger the second link

This uses the :eq() selector. You could also use the .eq() method if you wanted.

function someFunction( n ){
    $(".links").eq( n ).trigger('click');
}
patrick dw
Thanks Patrick. I really appreciate your help. This is exactly what I was looking for.
Chris Chiera
@Chris - You're welcome. :o)
patrick dw
+2  A: 

To trigger for the second link only:

$(".links").eq(1).trigger('click');

.eq(n) reduce the set of matched elements to the one at the specified index. The index is zero based.

Arrix