Reputation: 3702
So I have acquired the element I want, but I want to check the elements parent class value to verify it is a certain value before doing my logic and I can't quite figure it out.
$(document).ready(function() {
$('#childElement input').click(function() {
var option = $(this).parent('span').class;
alert(option.class);
if (option == 'left') {
//some action
}
});
The HTML is:
<span class="left">
<input type="checkbox" tabindex="401"><label>some verbage</label></span>
I am able to fire on the checking off of the checkbox, but I want to acquire the 'class' value of the span element (which i believe is the input element's parent. Then do an if comparison to check if class == "left".
Any thoughts what I am doing wrong?
Upvotes: 3
Views: 1105
Reputation: 630379
You can check using .hasClass()
, like this:
if($(this).parent('span').hasClass('left')) {
Or use a .class
selector and .length
, like this:
if($(this).parent('span.left').length) {
To get the class
like you were attempting, it would be .attr('class')
instead of .class
:)
Upvotes: 4