Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate anchor link click

Tags:

jquery

anchor

I'm trying to trigger a link click for .jquery. Does someone know why the following doesn't work.

<!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html lang="en"> 
  <head> 
    <title>test</title>
  </head>
  <body> 
    <div>
       <a id="google_link" href="http://google.com" target="_blank">click to go to google</a>
    </div>
    <div id="google_link_proxy">click here to do the same as the link above</div>

    <script type="text/javascript">      
     $("#google_link_proxy").click(function(event){
         $("#google_link").click();
     });
    </script>
  </body>
</html>
like image 565
RayLoveless Avatar asked Dec 02 '10 17:12

RayLoveless


People also ask

How do you trigger an anchor click?

If you are trying to trigger an event on the anchor, then the code you have will work I recreated your example in jsfiddle with an added eventHandler so you can see that it works: $(document). on("click", "a", function(){ $(this). text("It works!"); }); $(document).

How do you simulate a click event?

Method 1: Using the click() method: The click() method is used to simulate a mouse click on an element. It fires the click event of the element on which it is called. The event bubbles up to elements higher in the document tree and fires their click events also.

How do you simulate a click in HTML?

The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.

Can I use onclick on anchor?

How does anchor tag handle click event? On using Google I found that they are using onclick events in anchor tags. It usually goes to 'more. To be more sure, use a # in href="#" and do the necessary things within the javascript.


5 Answers

The jQuery method completely ignores href:

$('#google_link').click(); // ignores href!

The native DOM method does the right thing:

$('#google_link')[0].click();

This works regardless of whether the href is a URL, a fragment (e.g. #blah) or even a javascript:.

like image 117
Roman Starkov Avatar answered Nov 06 '22 14:11

Roman Starkov


If you use jQuery and the native DOM, the anchor can be clicked

// insert an <a> into document and click it **natively**
// (.get(0) returns the DOM element)
$('<a id="fred99" />').attr('href', '#david').attr('target', '_blank')
.text('LINK').appendTo('body').get(0).click();

// now we've clicked, tidy up
$('#fred99').remove();
like image 36
Wandering Zombie Avatar answered Nov 06 '22 15:11

Wandering Zombie


Looks like your $("google_link_proxy") selector is off. Try $("#google_link_proxy").

You also need to close the observe call with }).

Those are the syntax errors with the code above though I don't think those functions are provided in jQuery by default.

Here is what I think you're after:

$("#google_link_proxy").click(function(event){
    window.open($("#google_link").attr('href'),'_blank')
});
like image 37
Jeff the Bear Avatar answered Nov 06 '22 13:11

Jeff the Bear


Make sure you have your jQuery code wrapped in a ready block like so

$(document).ready(function(){/* your code here */});

This ensures scripts are fired after all the content and images are loaded.

like image 2
pixelbobby Avatar answered Nov 06 '22 13:11

pixelbobby


Use click()

$("#google_link_proxy").click(
    function(){
        $("#google_link").click();
    }
);

fireEvent and observe is not part of jQuery API

like image 2
epascarello Avatar answered Nov 06 '22 14:11

epascarello