jquery click doesn't work on hyperlink
Asked Answered
C

7

18

I have a simple link click simulation that I want to do using jQuery. From what I read, this should work, but the code below doesn't work. Nothing happens if I do the same thing as a part of some other event or something either. Thoughts?

<script type="text/javascript">
  $(function() {
    $("#lnk_0").click();
  });
</script>

<a id="lnk_0" href="http://mydomain.com/mypage.html">link</a>
Chalcanthite answered 13/1, 2010 at 0:42 Comment(0)
T
26

See click():

Triggers the click event of each matched element.

Causes all of the functions that have been bound to that click event to be executed.

The important thing to note is that it does not duplicate clicking the link. It only triggers associated events. If you want to change location:

var link = $("#link_0");
link.click();
window.location.href = link.attr("href");

but even that is only an approximation as it doesn't cater for handlers stopping event propagation.

Trabzon answered 13/1, 2010 at 0:44 Comment(1)
Okay, thanks for clarifying that. I've actually spent some time staring at that exact documentation. I would think that navigating to the new location qualifies as an "associated event." I guess, technically, it's not? :/ I will accept this answer, but SLaks input below is also very helpful. Let this stand as the answer to programmers once and for all! $("#my_hyperlink").click() will not cause the browser to navigate!Chalcanthite
D
9

Calling jQuery's click method will invoke any click handlers that you've added, but will not click the link.

You need to write:

window.location = $("#lnk_0").attr('href');

(This assumes that there aren't any click event handlers)


EDIT: In response to your comment, you can call the IE-only DOM click method, like this:

if ($("#lnk_0")[0].click)
    $("#lnk_0")[0].click();
else
    window.location = $("#lnk_0").attr('href');
Durfee answered 13/1, 2010 at 0:46 Comment(1)
SLaks, thanks for your solution. Only problem is it has the really annoying side effect of losing the Referer URL header in IE, which is what I'm trying to prevent.Chalcanthite
X
4

To allow the hyperlink functionality (hover, hand cursor, etc) when overriding with jQuery you can do the following:

<script>
    $(function() {
        $( "#dialog-modal" ).dialog({
            autoOpen: false,
            height: 320,
            modal: true
        });

        $( "#open-modal" ).click(function() {
            $( "#dialog-modal" ).dialog( "open" );
        });         
</script>
<a href="javascript:return true" id="open-modal">Open Modal</a>

<div id="dialog-modal" title="Hello World">
Hello
</div>
Xylem answered 21/10, 2012 at 11:12 Comment(0)
H
2

Doing this works:

$('#lnk_0')[0].click();
Histrionism answered 28/8, 2019 at 3:33 Comment(1)
The reason why I find this useful is because it defaults to document.getElementById("lnk_0").click();.Fitzger
E
0

Just use

document.getElementById('#lnk_0').click();

or

$("#lnk_0")[0].click();
Encephalitis answered 27/11, 2022 at 6:3 Comment(0)
B
0

This one worked for me, include the $("#lnk_0").click() inside the $(document).ready handler:

$(document).ready(function () {
    $("#lnk_0").click(function (e) {
      //your code here
    });
});
Baccate answered 13/4, 2023 at 17:33 Comment(0)
F
-1

I am not sure it solves your precise problem, but here is a snippet of code I use to simulate a user click:

var target = "...";

var e = jQuery.Event("click");

target.trigger(e);

if (!e.isDefaultPrevented() && target.attr("href"))
    window.location.href = target.attr('href');
Freddie answered 20/9, 2012 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.