I have the following html:
<div id = "CompOne">
<div id="btns">
<input type="button" value="Add" class="cartbtn" onclick="SetSubmitBundleValue(this)">
</div>
</div>
<div id = "CompTwo">
<div id="btns">
<input type="button" value="Add" class="cartbtn" onclick="SetSubmitBundleValue(this)">
</div>
</div>
And I want to get value of the parent div CompTwo/ CompOne on their button click. Is it possible to retrieve this value from the object passed through the 'this' keyword? Kindly help
function SetSubmitBundleValue(obj)
{
//Get Parent div
}
You can use parent() method:
$('.cartbtn').on('click', function(){
var parent_id = $(this).parent().parent().attr('id');
console.log(parent_id);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "CompOne">
<div id="btns">
<input type="button" value="Add" class="cartbtn">
</div>
</div>
<div id = "CompTwo">
<div id="btns">
<input type="button" value="Add" class="cartbtn">
</div>
</div>