0x49D1
0x49D1

Reputation: 8704

JQuery click on link not working

Ive the following code:

<script type="text/javascript">
$(document).ready(function(){
    shortcut.add("Ctrl+Alt+N", function() {
        $("#btnSave").click();
    });
});
</script>

where btnSave is anchor element with ID btnSave, shortcut is from http://www.openjs.com/scripts/events/keyboard_shortcuts/ . If i change the line $("#btnSave").click(); to document.getElementById("btnSave").click() - all works fine. The question is why jquery implementation is not working in my case?
PS: made jsfiddle for my case: http://jsfiddle.net/0x49D1/WCmeU/
Here is the guy with similar problem: http://forums.asp.net/t/1591818.aspx

Upvotes: 12

Views: 22300

Answers (2)

soonji
soonji

Reputation: 79

try this

<script type="text/javascript"> 
      $(document).ready(function(){
         shortcut.add("Ctrl+Alt+N", function() {
            $("#btnSave").live('click',function(){
           // do stuff here
            });
         }); 
      }); </script>

Upvotes: 0

Instead of $("#btnSave").click(); try with $("#btnSave").trigger('click');

You can also use $("#btnSave")[0].click(); which is jquery equivalent to document.getElementById("btnSave").click();

Update:
It's not possible to simulate a user link click from javascript, for security reasons, all you can do is attach your own handler for click event and redirect based on the href of the link, like so:

$("#btnSave").bind('click', function() {
    window.location.href = $(this).attr('href');
});

Upvotes: 21

Related Questions