user5060975
user5060975

Reputation:

Click link after click another link

There is a way to simulate a click on a link after having clicked another?

I have made this script:

$(".uno").click(function() {
    $(".due")[0].click();
});

Fiddle: https://jsfiddle.net/5ad3m8La/

When I click on "one" want "two" open the page as indicated. My script does not work, and I do not understand why.

Upvotes: 1

Views: 1207

Answers (4)

Jayesh Chitroda
Jayesh Chitroda

Reputation: 5049

Try this:

<a href="#" class="uno">One</a>
<a href="http://www.google.com" class="due">Two</a>

jQuery:

$( ".uno" ).click(function() {
    $('.due').trigger("click");
});

$( ".due" ).click(function() { 
    var link= $(this).attr("href"); // you need to ger href and redirect to that link
    window.location.href = link;
});

Upvotes: 0

Ankush Jain
Ankush Jain

Reputation: 7049

Update your fiddle. Updated one https://jsfiddle.net/5ad3m8La/2/

Made few changes given below:

1. Added target="blank" in link.
2. Added http:// before google's link.
3. Wrapped your code in document.ready.

and it's working now :)

Upvotes: 1

Mhd Wael Jazmati
Mhd Wael Jazmati

Reputation: 667

Try the code below:

$('.due').trigger('click');

Upvotes: 0

online Thomas
online Thomas

Reputation: 9381

$(".uno").click(function(e) {
  e.preventDefault();
  location.href = $('.due').attr('href');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="uno">One</a>
<a href="google.it" class="due">Two</a>

This is not the same as emulating a click, but this way the behaviour is the same as clicking another linke (without special code attached to it)

Upvotes: 0

Related Questions