jQuery get id of element by searching for it by class
问题 This is my html : <div id="my_box_one" class="head-div"> <div> <div class="some_box">a</div> <div class="some_box">b</div> </div> </div> I want to get the ID of the parent div("#my_box_one") using the class of that div(".head-div") $(document).ready(function(){ $(".some_box").click(function(){ var abc = $(this).parentsUntil(".head-div").attr("id"); // also tried $(this).parent(".head-div") -- same effect alert(abc); // Shows as Undefined }); }); I can do the following and it will work okay,