sirweatha
sirweatha

Reputation: 27

jQuery toggle background color on radio buttons

I have a two radio controls that are designed as buttons. When I click the first radio button, then second one's background color should change and vice versa.

HTML:

<input type="radio" id="boo1" name="boo" value="boo1"><label for="boo1"><span></span>boo1</label>
<input type="radio" id="boo2" name="boo" value="boo2"><label for="boo2"><span></span>boo2</label>

CSS:

body {
font-family: open sans, arial, sans-serif;
color: #ffffff;
margin: 0;
background: #3894db;
}
input[type="radio"] {
display: none;
}
input[type="radio"] + label {
cursor: pointer;
width: 50%;
height: auto;
font-size: 22px;
margin-top: 10px;
float: left;
color: #ffffff;
text-align: center;
vertical-align: middle;
padding-top: 2px;
padding-bottom: 2px;
}
/* boo1 */
#boo1 + label {
background: #e74c3c;
}
#boo1:checked + label {
background: #c0392b;
}
/* boo2 */
#boo2 + label {
background: #f1c40f;
}
#boo2:checked + label {
background: #f39c12;
}

JavaScript?

$(document).ready(function() {
$("#boo1").click(function () {

});
});

Upvotes: 1

Views: 5406

Answers (7)

Aylen
Aylen

Reputation: 3554

If you want the buttons to have different background colors when "inactive", you can use jQuery's css() function, like this:

$(document).ready(function () {
    $("#boo1").click(function () {
        $("#boo1 + label").css("background-color", "#c0392b");
        $("#boo2 + label").css("background-color", "green");
    });
    $("#boo2").click(function () {
        $("#boo2 + label").css("background-color", "#f39c12");
        $("#boo1 + label").css("background-color", "blue");
    });
});

See working jsFiddle.

Upvotes: 1

Ahsan Khurshid
Ahsan Khurshid

Reputation: 9469

Simple and optimized solution:

HTML Code:

<!-- Fieldset Wrap: begins -->
<div class="fieldset-wrap">

    <!-- Fieldset: begins -->
    <span class="fieldset orange">
        Label 1
    </span>
    <!-- Fieldset: ends -->

    <!-- Fieldset: begins -->
    <span class="fieldset green">
       Label 2
    </span>
    <!-- Fieldset: ends --> 

</div>
<!-- Fieldset Wrap: ends -->

CSS:

.fieldset-wrap {
    overflow: hidden; 
    font-size:0px;
}

.fieldset { 
    display: inline-block; 
    width: 200px;
    padding: 5px 0;
    text-align: center;
    cursor: pointer;
    font-family: verdana;
    font-size: 14px;
 }

.fieldset { margin-left: 1px; }
.fieldset:first-child { margin-left: 0px; }

.orange,
.green,
.checked{ color: #fff; }

.orange { background-color : orange; }
.green { background-color : green; }
.checked { background-color : blue; }

jQuery:

$('.fieldset').on('click', function(){
    $(this)  
    .addClass('checked')
    .siblings()
    .removeClass('checked');
});

DEMO

Upvotes: 0

Harish Boke
Harish Boke

Reputation: 557

You can achieve that with pure CSS

/* boo1 */
#boo1 + label {
background: #e74c3c;
}
#boo1:checked ~ label[for="boo2"] {
background: #c0392b;
}
#boo1:checked ~ label[for="boo1"]{
    background: #f39c12;
}
/* boo2 */
#boo2 + label {
background: #f1c40f;
}
#boo2:checked ~ label[for="boo1"] {
background: #f39c12;
}

working demo:http://jsfiddle.net/HarishBoke/S6z8Z/

Hope this is what you are looking for!

Upvotes: 0

dreamweiver
dreamweiver

Reputation: 6002

try this way

HTML CODE:

      <label for="boo1"><span><input type="radio" id="boo1" name="boo" value="boo1" /></span>boo1</label>
      <label for="boo2"><span><input type="radio" id="boo2" name="boo" value="boo2" /></span>boo2</label>

JQUERY CODE:

        $('input[type=radio]').on('click', function () {
             $('input[type=radio]').not(this).closest('label').css('background', 'blue');
             $(this).closest('label').css('background', 'red');
        });

LIVE DEMO:

Happy Coding :) http://jsfiddle.net/dreamweiver/py2BM/14/

Upvotes: 0

111
111

Reputation: 1779

Try to use this javascript code

$(document).ready(function () {
    $("input[name='boo']").click(function () {
        $(this).next().removeAttr('style');
        $("input[name='boo']").not(this).next().css('background-color', 'green');
    });
});

Upvotes: 0

Sarath
Sarath

Reputation: 608

try the following code

$("#boo1").click(function (event) {

                $("#boo2").prop( "checked",true );
            });

            $("#boo2").click(function (event) {

                $("#boo1").prop( "checked",true );
            });

Upvotes: 0

Tanveer Shaikh
Tanveer Shaikh

Reputation: 1688

if($('#boo1').is(':checked'))
{
     $('#boo2').css('color','#ff0000');
}

Upvotes: 2

Related Questions