views:

131

answers:

1

Hi All

I have a list of 3 radio buttons with the 1st radio that's selected at load time. The 3rd button has a group of checkboxes related to it. When one of the checkboxes is checked the related radio button should be selected automatically. The problem is that this action doesn't work in Chrome of Safari but works fine in FF, Opera and even on IE (if IE can do it...). I'm using ajax, so when the page refreshes it goes back to the 1st radio being selected, which cancels the action, this only occurs in Chrome and Safari.

What's going on? Someone help me please...

Here's the code:

jquery + Ajax:

$.ajax({
type: "POST",
dataType: "text",
url: "ajax/possibleValues.html",
data: $("form#orderDefinition").serialize(),
success: function(response){
    $('#usercontent .sleeve .toprow').html(response);
    //alert(response);
    applyValidation();
    radioButtonHighlightSelection();
},
error: function(response, ioArgs, err) {
    if (response.status == 601) {
        sessionTimedOut();
    }
}         
});

$("#chooseSource input:radio").each(function(e){
    if($(this).is(":checked")){
        $(this).parent().addClass("selected");
    }
});

$("#chooseSource input:checkbox").each(function(){
    if($(this).is(":checked")){
        $(this).parent().parent().prev().find("input:radio").attr("checked", true);
        $(this).parent().parent().prev().find("input:radio").parent().addClass("selected");
        $(this).parent().parent().addClass("selected");
    }
});

Html:

<form id="optionForm">
    <div id="chooseSource">
        <div>
            <input type="radio" id="source1" name="source" value="www" checked="checked" />
            <label for="source1">Website</label>
        </div>
        <div>
            <input type="radio" id="source2" name="source" value="mag" />
            <label for="source2">Magazine</label>
        </div>
        <div>
            <input type="radio" id="source3" name="source" value="per" />
            <label for="source3">Friend</label>
        </div>
        <div>
            <input type="radio" id="source4" name="source" value="oth" />
            <label for="source4">Other</label>
        </div>
        <div id="cbHolder">
            <span>
                <input type="checkbox" id="sourceCb1" name="sourceCb" value="" />
                <label for="sourceCb1">Checkbox item 1 for other</label>
            </span>
            <span>
                <input type="checkbox" id="sourceCb2" name="sourceCb" value="" />
                <label for="sourceCb2">Checkbox item 2 for other</label>
            </span>
            <span>
                <input type="checkbox" id="sourceCb3" name="sourceCb" value="" />
                <label for="sourceCb3">Checkbox item 3 for other</label>
            </span>
        </div>
    </div>
</form>

Thanks

+1  A: 

http://jsfiddle.net/YuCQR/1/

Too long to post entirely, but this should work in all browsers. Here's the JS, I fixed some stuff for you:

$("#chooseSource input:radio").change(function(e){
    $("#chooseSource input:radio").parent().removeClass("selected")
    $(this).parent().addClass("selected");

});

$("#chooseSource input:checkbox").change(function(){
   //toggle class for this select box
   $(this).parent().toggleClass("selected");

   if($(this).is(":checked")){
    //remove all classes from radio boxes
     $("#chooseSource input:radio").parent().removeClass("selected")
     $(this).parent().parent().prev().find("input:radio").attr("checked", true);
     $(this).parent().parent().prev().find("input:radio").parent().addClass("selected");

   }
});​
Litso
@Litso: Lol... I thought it was long... Wow, your code looks like it's gonna work, so I'm gonna try it now and give you feedbacks. Hmm, so it's something to do with the 'change' event then.
Shaoz
Yeah, your code only ran when the site loaded and then never did anyhting again. with `.change()` the code activates each time one of the radios/checkboxes changes :) (it's like the `onchange=".."` in html). I was referring to the html when I said long, but I only changed 1 thing in the html (a default `class="selected"` on the first radio) so ignore that remark :P
Litso
@Litso: It works but because I'm also using ajax, when the browser refreshes, only the selection is made but the CSS class doesn't stay. It appears and then disappears again. But I want it to stay. Maybe I'm missing something. Also when the checkboxes is selected, the related radio also is selected and then unselected. Something to do with the browser refresh.
Shaoz
Hmm, maybe you should put the class toggles in a separate function and bind that both to the change event and the pageload event.
Litso
No, it still doesn't work. I even tried the live(), but no good result.
Shaoz
Do you have a live example? My example in jsfiddle works so it'd be useful if I can see what goes wrong in live action :)
Litso
The web app is not online yet. So I cannot show you a live example, but I can show the ajax code that's talking to the server. See above in my question.
Shaoz