0

I know this might be very simple but I can't get it to work. I am trying to trigger a click on a on page load. Below is the code I am using.

HTML

<button class="triggerme"></button>

JQUERY

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

I can't change it to an input, or anchor.

10
  • you dont have a click event what is there to trigger? Commented Jun 2, 2017 at 5:55
  • No, it should just trigger on load. Commented Jun 2, 2017 at 5:56
  • maybe it's on a form, then default action is submit? Commented Jun 2, 2017 at 5:56
  • have you tried .click()? ---> $( ".triggerme" ).click(); Commented Jun 2, 2017 at 5:57
  • @MathijsSegers is not in a form, it's just a button that opens up a dialog box. Commented Jun 2, 2017 at 6:00

3 Answers 3

1

$(document).ready(function(){
   $( ".triggerme" ).click(function() {
     alert( "You clicked on triggerme" );
    });
});
<head>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<button class="triggerme btn btn-primary">Click Me</button>

try this..

Sign up to request clarification or add additional context in comments.

4 Comments

this works when you click it, but does not tigger click on page load.
if you want to make it happen automatically when page loads then just replace with this $(document).ready(function(){ setTimeout(function() { $( ".triggerme" ).trigger('click'); alert( "You clicked on triggerme" ); },1000); });
try this.. it will work when page loads and you can give Timeout also as much you want..
Will do later. Lights went off.
0

Try this code.

$( ".triggerme" ).click(function() {
  alert( "Handler for .click() called." );
});

Comments

0
$(document).ready(function(){
  $(".triggerme").click()
    {
    enter code here to execute on button press
    }
});

1 Comment

Welcome to Stack Overflow! Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.