12

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

2
  • 1
    Where and how do you bind the click event for #btnSave? Commented Sep 27, 2012 at 9:15
  • @VisioN, i want to simulate user's click on link. Actually this is part of asp.net code and click on the link runs inokes some server event. Still, see my added JSFIDDLE sample. And if the case is with click binding, why document.getElementById("btnSave").click(); is working? Commented Sep 27, 2012 at 9:23

2 Answers 2

21

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');
});
Sign up to request clarification or add additional context in comments.

11 Comments

What you want to do is not possible, but you can kind of simulate it, see my update.
Then why shortcut.add("Ctrl+Alt+N", function() { //$("#btnSave").click(); document.getElementById("btnSave").click(); });​ works?..Think im missing something :( jsfiddle.net/0x49D1/WCmeU/13
What is forbidden is simulate following the link adress, if the link had href="google.com" instead of href="javascript: alert('tada')" your code wouldn't follow to google.
see new update for a jquery equivalent for your javascript click code.
@0x49D1 What I said about forbidden following link address is true, but only for external links, because they will be violating same-origin policy, if the link href is some page in the same host/port/protocol as the origin page, then calling .click() will follow the link. As documentation see here: en.wikipedia.org/wiki/Same_origin_policy
|
0

try this

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

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.