2

I have a list with checkboxes like this:

<ul class="cate_select_ul">
    <li>
        <!-- outer checkbox A -->
        <label><input value="251" type="checkbox" name="post_category[]">Automobiles &amp; Transport</label>
        <ul class="children">
            <li>
                <!-- inner checkbox A -->
                <label><input value="252" type="checkbox" name="post_category[]">Car Parts</label>
            </li>
        </ul>
    </li>
    <li>
        <!-- outer checkbox B -->
        <label><input value="251" type="checkbox" name="post_category[]">Automobiles &amp; Transport</label>
        <ul class="children">
            <li>
                <!-- inner checkbox B -->
                <label><input value="252" type="checkbox" name="post_category[]">Car Parts</label>
            </li>
        </ul>
    </li>

</ul>

I want to check if the inner checkbox is checked or not. If so I want to change the style of the label around the parent outer checkbox. I tried this but it won't work properly:

if($('ul.children input[name="post_category[]"]:checked').length > 0){
$("this").parent().parent().parent().parent().children("label").css({"color":"red"});
}

Any ideas on how to make this work?

1
  • Where did you put your snippet in? You should make sure what you can get from $(this). Commented Feb 15, 2016 at 3:06

1 Answer 1

4

Your example isn't working because $("this") attempts to select an element with a tag type of <this>. Since that element doesn't exist, nothing is selected.

Normally, $("this") should be $(this) (since this is a keyword and not a string), however in your case, it doesn't refer to the element that you think it does because it doesn't look like there is any scope. In your case, the variable this probably refers to the window object; you can always check with console.log(this).


As for a solution, you could iterate over the elements using the .each() method in order for this to refer to the current checked input element. You also don't need to chain the .parent() method four times since you can use the .closest() method in order to select the specified closest ancestor:

Example Here

$('ul.children input[name="post_category[]"]:checked').each(function() {
  $(this).closest('.children').prev('label').css('color', '#f00');
});

Of course you don't actually need to use .each() method since you can just select the elements directly.

In the line below, the :has() selector is used in order to select ul.children elements that have checked input[name="post_category[]"] descendant elements. From there, the previous label element is selected and the corresponding CSS is changed:

Example Here

$('ul.children:has(input[name="post_category[]"]:checked)').prev('label').css('color', '#f00');

As a side note, if you want to put this in a change event listener, it would look something like this:

Updated Example

$('ul.children input[name="post_category[]"]').on('change', function() {
  $(this).closest('.children').prev('label').toggleClass('selected', this.checked);
});
Sign up to request clarification or add additional context in comments.

8 Comments

Not only is this a correct answer. I was able to learn so much! Thank you very much!!!
Dear Josh, when I tried to change "has" by "not" to color just the labels of the children checkbox lists who are empty, all the outer labels became red. =S Any hints?
@mesqueeb Then I'm pretty sure you want to wrap :not() around :has() like $('ul.children:not(:has(...))'). So it would be $('ul.children:not(:has(input[name="post_category[]"]:checked))').prev('label').css('color', '#f00'); -> updated example.
Thanks Josh! I got exactly what I needed with a combination of 3 of your examples: jsfiddle.net/nqcabpzo Would you deem this code good to your standards? ^^
@mesqueeb Yeah; that looks fine.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.