Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8947533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:47:13+00:00 2026-06-15T12:47:13+00:00

http://jsfiddle.net/2Wpfw/16/ This seems really trivial question but I cannot for the life work this

  • 0

http://jsfiddle.net/2Wpfw/16/

This seems really trivial question but I cannot for the life work this one out.

I have this jQuery var…

var $selectArea = $("ul#1 li input[type='checkbox'],
ul#2 li input[type='checkbox'], ul#3 li input[type='checkbox']");

and I want to run this…

$selectArea.closest('li').addClass('box-checked');

but only if the $selectArea attribute checked is "checked" – pretty simple right?

So I’ve tried this…

if ( $selectArea.attr("checked") == "checked" ) {

    $selectArea.closest('li').addClass('box-checked');

}

and…

$selectArea.filter('[checked="checked"]').closest('li').addClass('box-checked');

and this… (but think im sniffing up the wrong tree here)

$selectArea.is('[checked="checked"]').closest('li').addClass('box-checked');

And ideas would be awesome thanks.

http://jsfiddle.net/2Wpfw/16/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:47:14+00:00Added an answer on June 15, 2026 at 12:47 pm

    It’s because you’re doing it inside the change event for the checkbox. Go ahead an check one of the checkboxes – it will have the class added.

    I would make the code that you want to happen to checked checkboxes a separate function so you can easily call it onload and onchange and have the same results.

    Heres an updated fiddle showing you what I mean.

    // reusable function
    function check($elem)
    {
        $elem.closest('li').toggleClass('box-checked', this.checked)
    }
    
    $selectArea.on('change', function() {
        check($(this));
    });
    
    $(function(){
        // onload get all checked inputs via the :checked selector 
        check($('input:checked'));
    });
    

    ​

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://jsfiddle.net/yTkTd/ Another function with a class works perfectly, but this one seems to be
http://jsfiddle.net/YdPPq/3/ I'm working locally, but I feel this should still work. How do I
http://jsfiddle.net/WTELW/4/ The following works fine on JSFiddle, but doesn't seem to work right in
http://jsfiddle.net/DaJWC/ i can't figure out how to make one of the links reverse so
http://jsfiddle.net/2Wpfw/3/ I'm not sure if my question title is the best description. I'm after
http://jsfiddle.net/4UbKe/3/ Please check out the fiddle to see exactly why it doesn't work. I'm
http://jsfiddle.net/xfCE8/3/ I've been trying to get less CSS to work, but with no luck,
http://jsfiddle.net/AndyMP/UpLts/1/ This is just an example for illustrative purposes, but what I want to
http://jsfiddle.net/NCt4D/ I want it to move (the image is large), but it dont work.
http://jsfiddle.net/nWqYL/2/embedded/result/ I've been working on fixing some issues with this prototype site. Myself and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.