0

I am working with this code: (Updated)

   <script>
$(function(){
//Radio Item 1
$("#radio1").change(function() {

$("#defaultwrapper").addClass("background_color_selected");
$("#default_wrapper").removeClass("def_background_color");

});

//Radio Item 2
$("#radio2").change(function() {

$("#default_wrapper2").addClass("background_color_selected");
$("#default_wrapper2").removeClass("def_background_color");

});



});
</script>

My problem is that I need the background-color of the selected radio to change and it's not toggling right now

2
  • 4
    Have you had a look at the documentation? api.jquery.com/toggleClass. It clearly says: "className One or more class names (separated by spaces) to be toggled for each element in the matched set.". Or, if you don't want both classes to be added, call toggleClass twice, or omit the flag. Commented Jan 3, 2012 at 10:16
  • With a boolean value, that will just add or remove both of them, though. Commented Jan 3, 2012 at 10:18

4 Answers 4

2
  $(function(){
        $("#myradio").change(function() {
    $("#wrapper").toggleClass("classOne", this.checked).toggleClass("classTwo", !this.checked)
        }).change();
    })
Sign up to request clarification or add additional context in comments.

Comments

1

(Update) In jQuery no event is fired when radio is unselected. Try to create global handler:

$(function(){
    $("input[type=radio]").change(function() {
        $("input[type='radio']", $(this).parent().parent()).each(function() {
            $(this).parent()
                .toggleClass("background_color_selected", this.checked)
                .toggleClass("def_background_color", !this.checked);
        });
    });
});

For html:

<div>
    <div class="def_background_color"><input type="radio" id="radio1" name="aaa" /> One</div>
    <div class="def_background_color"><input type="radio" id="radio2" name="aaa" /> Two</div>
</div>

Working sample: http://jsfiddle.net/THrYJ/

Comments

1

If you have classOne present at first (f.e. <div class="classOne" id="test">Text</div>) you can switch classes like this: $("#test").toggleClass("classOne classTwo");. This will remove the class classOne (because its present yet) and it will add the class classTwo (as its not present yet).

Comments

1

Hi i had the same problem but this is what i have done in my project...

    <script>
    $(".rtPanhold01").addClass("rtPanhold02");
        $(".rtPanhold01").removeClass("rtPanhold01");
</script>

Comments

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.