Skip to content Skip to sidebar Skip to footer

Click Anchor Tag Link On Enter Press

I have an anchor tag like Continue It's inside a pop-up. I need to click on

Solution 1:

I think the problem is that you're using event.keyCode, which isn't always used in all browsers. Some browsers use event.charCode or even a different event.which, which may be supported by what you're using. Anyways, the normal way to get the keycode from the event with jQuery is to use event.which.

jQuery normalizes the event object passed to the event handler and fixes "problems" like this so that you don't have to worry. At the same time, it seems that it copies over some of the original event's properties ("Most properties from the original event are copied over and normalized to the new event object." - from the jQuery API docs). That's probably why it's "working" for the other people commenting/answering. The event parameter passed to the handler has been generated/normalized by jQuery and will have everything you need, using the right properties. The correct way though, is to use event.which to get a normalized keycode for the event. http://api.jquery.com/event.which/

$(document).ready(function () {
    $(document).on("keyup", function (event) {
        if (event.which == 13) {
            $("#clicking").trigger('click');
        }
    });
});

Solution 2:

I've created this JSFiddle: http://jsfiddle.net/egzsf/ It works perfectly I only added a fallback for Internet Explorer.

What does your popup looks like? Maybe it's an iFrame, that would be a logical explaination.

Code:

<a class="btn btn-danger" id="clicking" data-bind="click: $root.enterLocation" onclick="alert('test')" href="#">Continue</a>

$(document).ready(function(){ 
    $(document).keyup(function(e){
        if (e.keyCode == 13){
        $("#clicking").trigger('click');       

        }
    })
});​

Better alternative is using e.which

Solution 3:

I hope following code can help you .try it Code:

<a class="btn btn-danger" id="clicking" onclick="window.location='index.php'" href="#">Continue</a>

$(function(){
  $('body').keyup(function(e){
    if (e.keyCode == 13){  
      $("#clicking").click();
    }
  })
});

Post a Comment for "Click Anchor Tag Link On Enter Press"