javascript - How to get inner id/class from div? -


this question has answer here:

i have this:

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

this html. inside of container, 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, want retrieve id inner divs (they 1, 2, 3) whenever click on of divs added using jquery, shows container or nothing @ all. how can id added divs? tried with:

$(".q").click(function(){     alert($(this).attr("id")); }); 

you can try this...

$( "#container" ).on( "click", ".q", function() {   alert($(this).attr("id")) }); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -