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, but it doesn't seem right.
var abc = $(this).parent("div").parent("div").attr("id");
You can use .closest( selector ), for example:
var abc = $(this).closest(".head-div").attr("id");
http://api.jquery.com/closest/
.parent( selector ) selects only immediate parent of the element.