'How to trigger click on page load?

I'm looking for a way to automatically "click" an item when the page loads.

I've tried using

$("document").ready(function() {
    $("ul.galleria li:first-child img").trigger('click');
});

but it doesn't seem to work? However, when I enter $("ul.galleria li:first-child img").trigger('click'); into Firebug's console and run the script, it works.

Can the trigger event be used on load?



Solution 1:[1]

$(function(){

    $(selector).click();

});

Solution 2:[2]

$("document").ready({
    $("ul.galleria li:first-child img").click(function(){alert('i work click triggered'});
}); 

$("document").ready(function() { 
    $("ul.galleria li:first-child img").trigger('click'); 
}); 

just make sure the click handler is added prior to the trigger event in the call stack sequence.

  $("document").ready(function() { 
        $("ul.galleria li:first-child img").trigger('click'); 
    }); 

   $("document").ready({
        $("ul.galleria li:first-child img").click(function(){alert('i fail click triggered'});
    }); 

Solution 3:[3]

You can do the following :-

$(document).ready(function(){
    $("#id").trigger("click");
});

Solution 4:[4]

try this,

$("document").ready(function(){

$("your id here").trigger("click");
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steven
Solution 2 Mark Schultheiss
Solution 3 Krish Bhanushali
Solution 4 user229044