I have a situation where an element loads on the page(dynamically added by some third party) and have defined certain event for that element.
Below is the code which mimics the same situation where the .on()
is not working on an element which I am adding dynamically.
What can be the issue here?
// 1
$('#bhansa').click(function(){
console.log('first');
})
// 2
$('.dom').on('click', '#bhansa', function(){
console.log('second');
})
setTimeout(function(){
$(".dom").html("<a href='#' id='bhansa'>Click me</a>");
}, 4000);
// 3
$('#bhansa').on('click', function(){
console.log('last');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="dom"></div>