jQuery .trigger('click')

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>

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');
}

.triggerHandler(), While .trigger() will operate on all elements matched by the jQuery object, .​triggerHandler() only affects the first matched element. Events triggered with . The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.

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.

Triggering Event Handlers, trigger()'d? jQuery's event handling system is a layer on top of native browser events. When an event handler is added using .on( "click"  jQuery - trigger( event, [data] ) Method - The trigger( event, [data] ) method triggers an event on every matched element.

The above solutions did not work for me.

But this solution works for me.

$('#elementID').click(function () {
    //some stuff
});

I need trigger to work when the page is loaded.

$( document ).ready(function() {
    $('#elementID').trigger('click');
});

OR

If you do not need to run when the page is loaded, you can use it in the function.

function someFunction(){
    $('#elementID').trigger('click');
}

jQuery trigger() Method, The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to  jQuery provides a way to trigger the event handlers bound to an element without any user interaction via the .trigger() method. link What handlers can be .trigger()'d? jQuery's event handling system is a layer on top of native browser events.

How to trigger a click on a link using jQuery, If you are trying to trigger an event on the anchor, then the code you have will work I recreated your example in jsfiddle with an added  Bind an event handler to be fired when the mouse leaves an element, or trigger that handler on an element. jQuery in Action Bear Bibeault,

jQuery, The trigger() method is a method in jQuery which is used to trigger a specified event handler on selected element. Syntax: $(selector).trigger(event, param1,  Apparently, calling $().trigger("ready") directly doesn't work because jQuery resets the ready event handlers after it runs them. You will need to keep track of the functions you need to call, and call them directly as below.

JQuery .trigger("click") event not the 'same' as a real 'click'?, I am trying to use the JQuery .trigger(“click”) event to do this and it… 'Sort-of' works… The code thus far is on start up the program will randomly  How to trigger jQuery change event in code. Ask Question Setting a value before actually triggering change event is the best way!! – Kapil Yadav Mar 18 at 6:12.

Comments
  • Thanks Patrick. I really appreciate your help. This is exactly what I was looking for.
  • how to do this for all?