<div id=id1>
<input class=class1>
</div>
<div id=id2>
<input class=class1>
</div>
Above is my html tags. in below code i have select the parent id like this,
$(".class1").parent().attr('id')
but i am getting id1 only .i want both id1 and id2. ?? what to do ?
as per docs:
.attr( attributeName ): Get the value of an attribute for the first element in the set of matched elements.
Thus, you need to use .map()
function to get all elements id along with .get()
to get them in array:
$(".class1").parent().map(function(){
return this.id;
}).get();
Working Demo
Try like this
$(function(){
$(".class1").each(function(){
var id = $(this).parent().attr("id");
console.log(id);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="id1">
<input class="class1">
</div>
<div id="id2">
<input class="class1">
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With