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
So Why Does It Happen? JQuery OnClick Method is bound to an element or selector on page ready/load. Therefore if that element you want to click isn't there at the time of page ready, the binding can't happen.
Introduction to jQuery click link. The jQuery click link is used to handle the click event when the click event occurs for the link element. The jQuery click() function and trigger() function we can use to trigger and handle the click on a link event programmatically in the jQuery.
On both Windows 10 and 7, head to Control Panel > Hardware and Sound > Mouse. Ensure the “Turn on ClickLock” option is unchecked here. It's possible that a hardware driver issue could be causing issues with recognizing your mouse button's clicks, too.
click(function () { $("#anchor_google")[0]. click(); }); First, find the button by type(using ":") if id is not given. Second,find the anchor tag by id or in some other tag like div and $("#anchor_google")[0] returns the DOM object.
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');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With