0

I have something like this:

<div id="container"></div>

this is the html. Inside of the container, I add other div elements using jQuery.

$(document).ready(function () {
    $.get("/Ask", {}, function (response, status) {
        $.each(response.cars, function (index, val) {
            $("#container").append("<div class = 'q' id=" + val.id + "\">" + val.carName + "<br>" + val.date + "</div>");
        });
    });

    $("div").click(function (e) {
        e.stopPropagation();
        alert($(this).attr("id"));
    });
});

On click, I want to retrieve the id from my inner divs (they are 1, 2, 3) but whenever I click on any of the divs added using jQuery, it shows container or nothing at all. How can I get the id from the recently added divs? Tried even with:

$(".q").click(function(){
    alert($(this).attr("id"));
});
Simply Me
  • 1,579
  • 11
  • 23

1 Answers1

1

you can try this...

$( "#container" ).on( "click", ".q", function() {
  alert($(this).attr("id"))
});
Tanvir
  • 1,635
  • 2
  • 17
  • 31