Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 861
0x49D1 Avatar asked Sep 27 '12 09:09

0x49D1


People also ask

Why click is not working in jQuery?

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.

How to click on link using jQuery?

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.

Why click is not working?

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.

How to trigger click event on anchor tag in jQuery?

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.


1 Answers

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');
});
like image 173
Nelson Avatar answered Oct 04 '22 06:10

Nelson