LoganPHP LoganPHP - 3 months ago 18
Javascript Question

How to fire hyper link through jquery trigger?

I want to fire hyper link through javascript. It link must be open in new tab.

We can use window.open('url') but we know browser blocks popup. So i need alternate solution for it.

I have tried with trigger() but it doesn't work.

My HTML code is :

<input id="btn" type="button" value="Click to fire">
<a id="link" href="http://www.google.com" target="_blank">Link</a>


My script is :

$('#btn').click(function(){
$('#link').trigger('click');
});


Besides i used click(), it doesn't work too.

Code is :

$('#btn').click(function(){
$('#link').click();
});


Can you any one help me?

Answer

Here is your solution - http://jsfiddle.net/svTWu/1/

Here is the code

<input id="btn" type="button" value="Click to fire">
<a class="thelink" href="http://www.google.com" target="_blank">Link</a>

$(".thelink").click(function () {
  window.open($(this).attr("href"));console.log('hello');
});

$("#btn").click(function () {
  $(".thelink").trigger('click');
});

Here you can read all information about trigger - http://api.jquery.com/trigger/