views:

21

answers:

1

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?

+4  A: 

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 :)

Nick Craver
Wow, was that ever easy.
pghtech
Also, `$(this).parent('span')[0].className;` :)
karim79
@pghtech - Did that resolve it, or are you still having issues? :)
Nick Craver
That resolved it. I couldn't select an answer until a period of time had passed.
pghtech