Reputation: 1315
I am trying to figure out how to find the id of particular div by class name and I'm not sure why my code does not reveal the id of the targeted div in a console (firebug). I would greatly appreciate any help.
Html
<button id="button">press</button>
<div class="div_1" id="1"></div>
<div class="div_2" id="2a"></div>
JQuery
$(document).ready(function() {
$('#button').click(function() {
var test= $(this).find('.div_2').attr('id');
console.log(test)
});
});
Upvotes: 0
Views: 53
Reputation: 7593
.find()
searches through of the the descendants of the calling selection.
In your case, you are calling it on the $(this)
on the click of a button. It is therefore going to look for child elements of the button element with a class div_2
of which there are none.
You can simply use the jquery selection to do the same thing without restricting the search to the child elements of the button element:
$(".div_2*").Attributes("id");
If you want to use .find
then you'd need to use the parent element that contains the divs you want to find...
Upvotes: 2