Jquery Can I fire a click event on a page when I reload the same?

How to make the function clickrun every time after the user reloads the page?

what I'm trying to do: run alert( "Handler for .click() called." );before user click,

HTML:

<p>First Paragraph</p>

JQuery

$( "p" ).click(function() {
  alert( "Handler for .click() after reload was called." );
});
+4
source share
5 answers

You can achieve this with a method click():

$( "p" ).click();

Another method is to use the method .trigger().

$( "p" ).click(function() {
  alert( "Handler for .click() after reload was called." );
}).trigger('click');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>First Paragraph</p>
Run codeHide result
+2
source

You need to trigger a click event for the paragraph for this Like

$("p").click();
+1
source

click() pageload, ;

var clickHandler = function() {
      alert( "Handler for .click() after reload was called." );
    }

, document.ready

$(document).ready(clickHandler);
$( "p" ).click(clickHandler);
+1

what you can do is to trigger the p click event when the document is loaded through the following code:

$( document ).ready(function() {
    $("p").click();
});

It is so simple.

+1
source

Plain..

$(document).ready(function(e) {
    $("#id-of-button-want-to-click-on-reload").trigger('click');
});

thank

+1
source

Source: https://habr.com/ru/post/1669817/


All Articles