tags:

views:

39

answers:

5

Greetings,

Being new to jQuery's syntax, I find myself being a bit thrown off when writing up some code. So while I realize my issue is very likely a logic issue, I'm still have issues finding it because of the syntax that I'm not used to.

I have a series of <li> tags that have an <input type="checkbox"> within each of them. When the page is refreshed, the checkboxes remain checked, however the <li> needs to keep the class of .grey attached still, but it loses it. Here is the code I'm trying to use at the moment to fix this issue:

$('#servemod').ready(function() {
    $('#servemod').find('li').each( function() {
    if ($('li').find('input:checkbox').attr('checked')==1) {
        /*alert($(this).find('input:checkbox'));*/
        /*alert($('li').find('input:checkbox').attr('checked'));*/
            $(this).addClass('grey');

    }
    });
});

I'm currently using the alert boxes to tell me exactly which part of the DOM the jQuery is currently looking at, and it seems okay. But the problem is that even with the loop, it only seems to test the first <li>'s checked state. If the first one is checked, the loop goes through and turns ALL <li>'s to blue. If the first one is not checked, it does nothing. I'm positive this is something very basic, but I've been at this for hours and I'm just not seeing it. Any help would be greatly appreciated.

The full code to the html section (with a lot of fluff pulled out)

<div id="servemod">
   <ul>
      <li><input type="checkbox"><p>box 1</p>
      <li><input type="checkbox"><p>box 2</p>
   </ul>
</div>

The css value for grey is just a colour change to blue.

+1  A: 

Does this do what you want it to do?

jQuery(
  function($)
  {
    $('#servemod li :checked').closest('li').addClass('grey');
  }
);

P.S: This is supposed to replace the entire code snippet you posted.

Thomas
Wow, what's with the downvotes? I figured I had a pretty good, concise answer. Chris's answer was also a legitimate pointer at the flawed mark-up, which can potentially break JS. I upvoted him, so at least 2 people downvoted his completely correct answer. WHat's up with that?
Thomas
A: 

Is it possible it's not working because you don't have the end input and li tags?

<li><input type="checkbox"><p>box 1</p></input></li>
Chris Conway
No, I have the proper html in my code, I just wrote that stuff up pretty fast so that people could see the idea.
mosquito
Oh, wow, totally missed that in my answer. Yes, you (OP) should fix your HTML. `<input />` is self-closing, but for the `<li>`, you need closing tags `</li>`. There's still a logical error in the if-clause, though. +1
Thomas
I have proper html in my actual page. :)
mosquito
+1  A: 
    $(document).ready(function() {
      $('#servemod ul li input:checked').each( function() {
        $(this).closest('li').addClass('grey');
      });
});
Detect
This does work, though it has messed up my click event. I'll keep fiddling to see if I can get them to work together.
mosquito
Got it! Thanks man!
mosquito
A: 

The test in your if statement operates on every checkbox inside an li element in your entire document:

$('li').find('input:checkbox') // returns all of the matches in the document.

Additionally, most jQuery methods operate on and apply to sets of elements, but not attr: it only operates on the first element in the of matched elements, so your if clause only tests the first checkbox contained inside an li.

Since you only want to check relative to the <li> element (and since each <li> only contains a single checkbox), you can use $(this):

if($(this).find('input:checkbox').attr('checked') == 1) {
    $(this).addClass('grey');
}
Jeff Sternal
+1  A: 

Here is the working code in jsfiddle http://www.jsfiddle.net/T4BKw/

$('#servemod li').each(function(){
    if($(this).find(':checkbox').is(':checked')){
        $(this).addClass('grey');
    }

});
Teja Kantamneni
This worked as well. I appreciate the help Teja!
mosquito