1

I'm facing an issue with a dynamically added link.

HTML

<button class="click-me">Click me</button>
<br><br><br>
<div class="dyanmic-link"></div>

JQuery

//Embedding a link dynamically
$('.dyanmic-link').html("<a href='www.google.com' class='dynamic-link'>Click Dynamically </a>");

//Click on a button 
$('.click-me').click(function(){
  $('.dynamic-link').click(); //Not working
  $('.dynamic-link').trigger( "click" ); //Not working
});

JSfiddle :- https://jsfiddle.net/e9ue227c/30/

3
  • check browser console for error and paste the same in question. Commented Apr 25, 2018 at 4:25
  • Thank you for your suggestion but I updated the question with JSfiddle link. Commented Apr 25, 2018 at 4:28
  • do you want that when click on '.click-me') the '.dynamic-link' will clicked ... why do you insert $('.dynamic-link').click(); to the '.click-me' function Commented Apr 25, 2018 at 4:33

1 Answer 1

2

click() doesn't directly work on a jQuery wrapped anchor. You need to pull out the native DOM object before you trigger a click. Replace this:

$('.dynamic-link').click(); //Not working

with this:

$('.dynamic-link')[0].click();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.