views:

218

answers:

1

I have the following html code:

<ul>
                <li class="O">
                    <label for="radio1">Radio 1:</label>
                    <input type="radio" name="radio1" value="Yes" checked>Yes</input>
                    <input type="radio" name="radio1" value="No">No</input>
                </li>
                <li class="O">
                    <label for="checkbox1">Checkbox 1:</label>
                    <input type="checkbox" name="checkbox1" id="checkbox1" value="check"></input>
                </li>
            </ul>

Using jQuery I'd like to do something with each checkbox not checked or element with an empty value inside an li element with class O. This is my approach:

$('li').each(function(){
        if ($(this).hasClass('O')){
            $(this).children(':not(label,input:radio)').each(function(){
                if ( $(this).val() == "" || $(this).is(':checkbox:not(:checked)') ) {
                    //do something
                }
            });
        }

This code is working on FF, Opera, Chrome and Safari, but not in IE6-IE7.

With both IEs, getting the length of the li's with $(this).children(':not(label,input:radio)').length returns 2 , other browsers return 0 and 1.

Also, getting the first li's number of children that are not a label ($(this).children(':not(label)').length) returns 4, while getting the number of children ($(this).children().length) returns 2. It makes no sense to me at all.

Am I wrong or is there something in the selector I used that is not fully compatible with IE?

Thanks in advance.

A: 

If I understand correctly you try to manipulate checkboxes that are not checked and are inside a li with class O. I have no IE to test, but I simplified it a bit. Can you try it?

$('li.O').each(function(){
    $(this).children(':checkbox:not(:checked)').each(function(){
        //stuff
    });
});

Update: Based on the comment, you can try this:

$('li.O').each(function(){
    $(this).children(':checkbox:not(:checked), :not(label,input:radio)[value=]').each(function(){
        //stuff
    });

});

zbrox
That works for checkboxes not checked, thank you. But what I want is to do something with checkboxes not checked OR elements without a value, that is the reason for including $(this).val() == "" in the inner if's clause. To sum up, I want to loop through elements that are not a label or a radio and if they have not a value or they are a checkbox not checked, do something.
ktMen
Thank you! That update works as desired!Question is: why doesn't work which I understand is kind of splitting the suggested selector in two clauses, that is, my approach?
ktMen
I'm not really sure. I just tried to simplify it. Whenever there's overcomplication of something chances are some problems will occur :) I'll take a deeper look later to test some stuff to see what may be the cause of the problem.
zbrox