Fire Click event in A-tag

11,881

Solution 1

First of all, you are using a selector that will match all <a> tags on your page. Perhaps you want to give your <a> an id attribute so that you can target one specific element...

<a href="foobar.html" id="foo">foo</a>

You could also try use the trigger function to simulate a click on the element -

$('#foo').trigger('click');

One last thing to mention is that you didn't actually call the method to open the link. You can try use this code -

jQuery('a').click(function(e){
   alert('It came here!');
   window.open($(this).attr('href'));    
   return false;   
});

Reference -

Description: Execute all handlers and behaviors attached to the matched elements for the given event type.

Solution 2

<div style="background-color:orange">Click me to open the link
    <br>
    <a id="b" href="http://example.com">Link</a>
</div>

jQuery('div').click(function(){
    document.getElementById('b').click();
});

fiddle: http://jsfiddle.net/zZqQs/4/

OR:

jQuery('a')[0].click();

Appears that jquery click and raw JS click differs from each other.

Solution 3

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>    
<div style="background-color:orange">Click me to open the link
    <br>
    <a href="http://example.com" target="_blank">Link</a>
</div>

<script>
    $('div').click(function () {
        $('a').click();
    });

    $('a').click(function (e) {

        alert('It came here!');
        e.stopPropagation();
        window.location = $("a").attr("href");
        return true;
    });
</script>
Share:
11,881
Mohammed H
Author by

Mohammed H

Bye Stack Overflow!

Updated on June 04, 2022

Comments

  • Mohammed H
    Mohammed H almost 2 years

    Possible Duplicate:
    Can I call jquery click() to follow an <a> link if I haven’t bound an event handler to it with bind or click already?

    I wrote the following code.

    <div style="background-color:orange">Click me to open the link
        <br>
        <a target="_blank" href="http://example.com">Link</a>
    </div>
    
    <script>
        jQuery('div').click(function(){
            jQuery('a').click();
        });
    
        jQuery('a').click(function(e){
           alert('It came here!');           
           e.stopPropagation();
           return true;   
        });
    </script>
    

    But the link is not opening.

    UPDATE: added target attribute to A-tag.

  • Mohammed H
    Mohammed H over 11 years
    Lix, To make the question simpler, I used the tag itself as selector.
  • Lix
    Lix over 11 years
    @hab - Ok... It just looked a little strange :)
  • Barmar
    Barmar over 11 years
    The jQuery documentation says that .click() is equivalent to .trigger("click").
  • Lix
    Lix over 11 years
    @bam - you are correct. But when things stop working, it never hurts to try different variants.
  • Mohammed H
    Mohammed H over 11 years
    See it in action jsfiddle.net/zZqQs/1 not working.
  • Mohammed H
    Mohammed H over 11 years
    I want to add 'target="_blank"' to the A-tag. will it work at that time?
  • Lix
    Lix over 11 years
    @hab - it seems to be working for me... What exactly is not working? Do you not see the alert?
  • Mohammed H
    Mohammed H over 11 years
    @Lix, it is not opening the link.
  • Lix
    Lix over 11 years
    @hab - perhaps that is because you didn't actually open any link... See this example. Clicking on both the link and the div work for me (in chrome)
  • Lix
    Lix over 11 years
    What is the reasoning behind using vanilla JavaScript if you are already in a jQuery function.
  • Mohammed H
    Mohammed H over 11 years
    @Lix, great! Please update the answer with this code.