Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  73] [ 4]  / answers: 1 / hits: 20881  / 12 Years ago, thu, september 27, 2012, 12:00:00

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


More From » jquery

 Answers
26

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');
});

[#82884] Wednesday, September 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grayson

Total Points: 36
Total Questions: 113
Total Answers: 95

Location: Tonga
Member since Fri, Aug 21, 2020
4 Years ago
grayson questions
;