Jquery .click() function not working when element added dynamically.


I am using dynamic element using below code.

This function is being called from ajax request and adding the <li> element.

function select(data) {
  $(".tag-search").before('<li><a class="label label-info">'+data+'&nbsp;&nbsp;<span class="badge text-icon">×</span></a></li>');

and then, I am using click function on it using below code.


But it is noty working.

asked 2 years, 2 months ago
Reputation: 1

1 Answer


jQuery .click() works great for most of the time, but we may encounter a problem when we add dynamic elements.

We can solve this problem using the code like below.

First find static tag in which that dynamic element exists and then use it like this. Here I am using .tag-conatiner

$(".tag-container").on('click', '.text-icon', function () {

or else we can use body tag also.

$("body").on('click', '.text-icon', function () {

answered 2 years, 2 months ago
Reputation: 1

Your Answer

Nothing to preview

Post Answer

Asked:  2 years, 2 months ago
Viewed:  698 times
Active:  2 years, 2 months ago