4

I'm having trouble getting the following to work:

$('#elem').trigger('click');

When I run it, it doesn't trigger a click but it doesn't show any error logs in the console either.

I've also tried the following with no luck:

$('#elem')[0].click();

Any ideas as to what could be going wrong or a solution for this?

Update: Jquery is working and when I inspect the element, #elem does appear (it's an id for a button)

HTML:

<a:button id="elem">My Button</Button>

JQuery:

P.when('A', 'jQuery').execute(function (A, $) {
    //when this function is called, it should trigger a button click
    triggerButtonClick = function() {
        $('#elem').trigger('click'); 
    };
});
7
  • 2
    Is element in the DOM when you trigger ? Commented Feb 12, 2016 at 5:58
  • are you sure the element exists when the code is run? Commented Feb 12, 2016 at 5:58
  • Make sure event handler is placed before the trigger. It works here Commented Feb 12, 2016 at 6:00
  • Do you have jQuery working? Do you have click handler attached? Make a fiddle otherwise. Commented Feb 12, 2016 at 6:00
  • 1
    try it n document.ready function Commented Feb 12, 2016 at 6:04

4 Answers 4

2

Try it in document.ready function, then all dom loads when the document is ready.You can do something like this based on your requirement.

$( document ).ready(function() {
    $('#radio').click(function(){
      $('#elem')[0].click();
  })	  
});

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

Comments

0

i have tried this code and it works

<!DOCTYPE html>
 <html>
 <head>
 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
 <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">     </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("chk1").click(function(){
    $('#usr').trigger('click');
  });
});
</script>
</head>
<body>
<form action="sample.php">
<button id="chk1">click</button>
<input class="form-control" type="submit" id="usr">
</form>
</body>
</html>

Comments

0

this code works for me:

$(window).load(function() {
    $('#menu_toggle').trigger('click');   
});

Comments

0

$(document).ready(function () {



        $('#elem').click(function () {
            alert('clicked');
            $(this).css('color', 'red');
        });

        $('#elem').trigger('click');

    });
<!DOCTYPE html>
<html>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<body>
 <div id="elem" >test</div>
 </body>
</html>

Comments

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.