I am dynamically creating the link on anchor tag through jquery, I want to trigger click event as soon as link gets attached to href
, but in my case click event didn’t get triggered.
JQuery:
$("#refShare").click(function(e){
jQuery.ajax({
url: base_url+"create-refer",
cache : false,
dataType: 'json',
data: { email:email },
type:'post',
success:function(response){
jQuery('li#refShare a:first').attr("href", 'https://www.xyzLink.com/'+response.link);
jQuery('li#refShare a:first').attr("target", 'blank');
jQuery('li#refShare a:first').click();
});
});
HTML:
<ul>
<li id="refShare">
<a href="" id="share-facebook">
<i class="fa fa-facebook fa-2x" aria-hidden="true"></i>
</a>
</li>
<li>
....
</li>
.....
</ul>
4
Answers
Try trigger
$("#refShare").trigger("click")
You need to choose the right element which can be clickable and your goal to redirect to somewhere else automatically(where human intervention does not require) can be achieved by using ‘trigger’ function.
you can do :
For further details/documentation about jquery ‘trigger’ function, you can check this url: .trigger()
Do you want an auto click or manual click?
If you want an auto =>
jQuery(‘li#refShare a:first’).trigger( “click” );
Otherwise manually means assign one id to this href and make it click.
jQuery(‘li#refShare a:first’).attr(“id”, ‘some_id’);
outside Ajax
Try this