Search code examples
javascriptjqueryjquery-click-eventjquery-trigger

jquery .click() does not work


on my php page I want to click a button on page load. I have tested it with jQuery and JS. While jQuery does not work, the JS works fine.

Any idea why this is the case?

jQuery:

<script>
$(document).ready(function() {
    $('#button_id').click();
    $('#button_id').trigger('click');
});
</script>

JS:

<script>
    window.onload = function() {
        document.getElementById('button_id').click();
    };
</script>

The button I want to click has a data-filter inside. Might this be the problem?

<button id="button_id" data-filter=".parkett" class="sub">Button</button>

EDIT:

No errors in the console, libary is added at the top.

This is my console output if I log both buttons:

Screenshot of console


Solution

  • $("document").ready(function() {
    setTimeout(function() {
        $("#button_id").trigger('click');
    },10);
    });