0

I add the button with the following code:

$('.dishes').prepend($('<div class="row" data-dishid="'+dishid+'"><div class="col-md-8">'+dishname+'<button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button></div></div>'));

But the following code doesn't catch the button click (when it works with the same code if it is part of the static html):

$("button.close").click(function() {
      var row = $(this).closest('.row');
      var dishid = row.attr('data-dishid');      
});

How can I fix that?

See the sample code.

1
  • 2
    It's obviously a duplicate and should be closed. Commented Apr 1, 2016 at 21:03

7 Answers 7

3

You need event delegation for dynamically generated element. Try like following.

$(document).on('click', "button.close", function () {
    var row = $(this).closest('.row');
    var dishid = row.attr('data-dishid');
    console.log(dishid);
});

WORKING DEMO

Sign up to request clarification or add additional context in comments.

Comments

3

For dynamically created elements, you should use .on('click', selector, callback)

like

$(document).on("click", "button.close", function() {
  var row = $(this).closest('.row');
  var dishid = row.attr('data-dishid');      
});

2 Comments

Thanks! I've edited my question - looks like the problem not only with reacting on 'click' event, but also with getting element's attribute.
@LA_ You also need to bind button.close with on
1
+50

I have seen the code. You have to call the click function of the dynamic element immediately after the element appended. Try the code below.

$("#add").on('click', function() {
    $('.dishes').prepend($('<div class="row" data-dishid="'+ "test" +'"><div class="col-md-8">'+ " test " +'<button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button></div></div>'));
    $("button.close").on('click', function() {
        var row = $(this).closest('.row');
        var dishid = row.attr('data-dishid');      
        console.log(dishid);
    });
});

Comments

0
$("button.close").on("click", function(){
   console.log('ok');
});

You need to use on() function for dynamically generated html elements

Comments

0

try:

$("button.close").on("click", function() {
  console.log('ok');
});

You need the .on event handler for dynamically created DOM elements.

UPDATE:

you don't need to put jQuery $ before the content of the prepend. Here is a working fiddle with your code.

https://jsfiddle.net/3k7Laohn/

1 Comment

Thank you. I use $ because I also animate the element addition (what I've removed from my sample code). But it really doesn't affect how it works. But your code doesn't work with my code, since I add the element on button click and not during page load - jsfiddle.net/3k7Laohn/1
0

Here is your updated code.

I changed:

$("button.close").click(function() {
      var row = $(this).closest('.row');
      var dishid = row.attr('data-dishid');      
});

To:

$(".dishes").on('click', 'button.close', function() {
      var row = $(this).closest('.row');
      var dishid = row.attr('data-dishid');      
      console.log(dishid);
});

This question is answered all over the internet and here is a snippet from jQuery's site:

Event handlers are bound only to the currently selected elements; they must exist at the time your code makes the call to .on(). Alternatively, use delegated events to attach event handlers.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.

Source

Comments

0

Upon checking your code, the click event should be working fine. Maybe the problem relies on the timing. My assumption is if you have the code below:

$("button.close").click(function() {
  var row = $(this).closest('.row');
  var dishid = row.attr('data-dishid');
}); 

You haven't appended the object yet on the DOM. Everytime you call the method below, you have to immediately bind the click event. Best is to have the codes together to check if it will work.

$('.dishes').prepend($('<div class="row" data-dishid="'+dishid+'"><div class="col-md-8">'+dishname+'<button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button></div></div>'));
$("button.close").click(function() {
  var row = $(this).closest('.row');
  var dishid = row.attr('data-dishid');
}); 

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.