1

I am trying to click a href element on my html website. Problem is that click event is not triggered

CODE:

HTML CODE:

  <div class="button" id="info" ></div>
<ul id="game-options">

    <li><a href="#" id="SHOW_HELP" class="button help" title="Help">HELP</a></li>
</ul>

Function that clicks a href element:

 $('#info').bind('click', function(e) {
            alert ("button click triggered");
    // This is what I tried so fat

     //$('#SHOW_HELP').trigger('click');
     //$('#SHOW_HELP').dispatchEvent(new Event('click'));
     $('#SHOW_HELP').click();
});
1
  • The question is, what are you exepecting when the anchor is clicked, it doesn't really seem to do anything ? Commented Aug 2, 2014 at 16:40

4 Answers 4

7

The click event is triggered, but you haven't bound a click handler so nothing will happen. If you want to simulate clicking the link you need to use the dom click method, ie. $('#SHOW_HELP')[0].click();

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

Comments

1

Alternatively, you can update the button's click event to change the window.location in JavaScript.

Try this:

$(function(){
    $('#info').click(function(){
        window.location = $('#SHOW_HELP').attr('href');
    });
});

Comments

0
$('#SHOW_HELP').bind('click', function(e) {
        alert ("button click triggered");
// This is what I tried so fat

 //$('#SHOW_HELP').trigger('click');
 //$('#SHOW_HELP').dispatchEvent(new Event('click'));

});

Demo

Hope it helps you

1 Comment

Don't use bind it is deprecated...!
0

It's actually working.

Here a JSFiddle example: http://jsfiddle.net/rZbk7/

$('#info').on('click', function(e) {
   alert("info triggered");
   $('#show-help').click();
});

$('#show-help').on('click', function(e) {
   alert("show-help triggered");
});

(not a big fun of snake case, I changed the name SHOW_HELP to show-help)

Click event is triggered. The problem is that you didn't define any action.

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.