jQuery function not binding to newly added dom elements

JavascriptJqueryOnclick

Javascript Problem Overview


Here's index.html:

<head>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
  <script type="text/javascript">
  
    $(document).ready(function() {
      $('.btn_test').click(function() { alert('test'); });
    });
    
    function add(){
      $('body').append('<a href=\'javascript:;\' class=\'btn_test\'>test</a>');
    }

  </script>
</head>
<body>
  <a href="javascript:;" class="btn_test">test1</a>
  <a href="javascript:;" onclick="add()">add</a>
</body>

If I click on test1 link, it shows alert('test'), but if I click on add link then click on test, it doesn't show anything.

Could you explain it?

Javascript Solutions


Solution 1 - Javascript

For users coming to this question after 2011, there is a new proper way to do this:

$(document).on('click', '.btn_test', function() { alert('test'); });

This is as of jQuery 1.7.

For more information, see Direct and delegated events

Solution 2 - Javascript

You need to use a "live" click listener because initially only the single element will exist.

$('.btn_test').live("click", function() { 
   alert('test'); 
});

Update: Since live is deprecated, you should use "on()":

$(".btn_test").on("click", function(){ 
   alert("test");
});

http://api.jquery.com/on/

Solution 3 - Javascript

I have same problem like question I was just near to pulling my hair then i got the solution. I was using different syntax

$(".innerImage").on("click", function(){ 
alert("test");
});

it was not working for me (innerImage is dynamically created dom) Now I'm using

$(document).on('click', '.innerImage', function() { alert('test'); });

http://jsfiddle.net/SDJEp/2/

thanks @Moshe Katz

Solution 4 - Javascript

.click binds to what is presently visible to jQuery. You need to use .live:

$('.btn_test').live('click', function() { alert('test'); });

Solution 5 - Javascript

Use Jquery live instead. Here is the help page for it http://api.jquery.com/live/

$('.btn_test').live(function() { alert('test'); });

Edit: live() is deprecated and you should use on() instead.

$(".btn_test").on("click", function(){ 
   alert("test");
});

Solution 6 - Javascript

This is because you click event is only bound to the existing element at the time of binding. You need to use live or delegate which will bind the event to existing and future elements on the page.

$('.btn_test').live("click", function() { alert('test'); });

Jquery Live

Solution 7 - Javascript

you need live listener instead of click:

$('.btn_test').live('click', function() {
alert('test');
});

The reason being is that the click only assigns the listener to elements when the page is loading. Any new elements added will not have this listener on them. Live adds the click listener to element when the page loads and when they are added afterwards

Solution 8 - Javascript

When the document loads you add event listeners to each matching class to listen for the click event on those elements. The same listener is not automatically added to elements that you add to the Dom later.

Solution 9 - Javascript

Because the event is tied to each matching element in the document ready. Any new elements added do NOT automatically have the same events tied to them.

You will have to manually bind the event to any new element, after it is added, or use the live listener.

Solution 10 - Javascript

$('.btn_test').click

will add the handler for elements which are available on the page (at this point 'test' does not exist!)

you have to either manually add a click handler for this element when you do append, or use a live event handler which will work for every element even if you create it later..

$('.btn_test').live(function() { alert('test'); });

Solution 11 - Javascript

After jquery 1.7 on method can be used and it really works nice

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
    </script>
<script>
    $(document).ready(function(){
      $("p").on("click",function(){
       alert("The paragraph was clicked.");
       $("body").append("<p id='new'>Now click on this paragraph</p>");
    });
    $(document).on("click","#new",function(){
       alert("On really works.");
      });
    });
</script>
</head>
<body>
    <p>Click this paragraph.</p>
</body>
</html>

see it in action http://jsfiddle.net/rahulchaturvedie/CzR6n/

Solution 12 - Javascript

Or just run the script at the end of your page

Solution 13 - Javascript

You need to add a proper button click function to give a proper result

$("#btn1").live(function() { alert("test"); });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRalsk28View Question on Stackoverflow
Solution 1 - JavascriptMoshe KatzView Answer on Stackoverflow
Solution 2 - JavascriptDoug MolineuxView Answer on Stackoverflow
Solution 3 - JavascriptUmer FarooqView Answer on Stackoverflow
Solution 4 - JavascriptCassOnMarsView Answer on Stackoverflow
Solution 5 - JavascriptAmir RaminfarView Answer on Stackoverflow
Solution 6 - JavascriptscrappedcolaView Answer on Stackoverflow
Solution 7 - JavascriptlocrizakView Answer on Stackoverflow
Solution 8 - JavascriptshanethehatView Answer on Stackoverflow
Solution 9 - JavascriptNeil NView Answer on Stackoverflow
Solution 10 - JavascriptKaroly HorvathView Answer on Stackoverflow
Solution 11 - JavascriptRahulView Answer on Stackoverflow
Solution 12 - JavascriptEnigmaxView Answer on Stackoverflow
Solution 13 - JavascriptSalman AshrafView Answer on Stackoverflow