0

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

Beji
  • 103
  • 1
  • 10

3 Answers3

0
$("document").ready(function() {
setTimeout(function() {
    $("#button_id").trigger('click');
},10);
});
mayur kasar
  • 165
  • 6
-1

$('#button_id').click();

You have defined no click handler - so nothing going to happen. You need to drop a function in there to use as the callback. Like this:

 $( "#button_id" ).click(function() {
    $(this).trigger('click');
 });
Korgrue
  • 3,430
  • 1
  • 13
  • 20
-2

Sounds like you haven't included the jquery script in your head

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
Seann
  • 81
  • 1
  • 6
  • libary is added in head. Please see my edited question. Thanks – Beji Mar 26 '18 at 19:20
  • Your jquery script should look like this: – Seann Mar 26 '18 at 19:22