Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery toggle div with radio buttons

Tags:

jquery

toggle

Simple html & jQuery

<label><input id="rdb1" type="radio" name="toggler" value="1" />Money</label>
<label><input id="rdb2" type="radio" name="toggler" value="2" />Interest</label>

<div id="blk-1" style="display:none">
    ...
</div>
<div id="blk-2" style="display:none">
    ...
</div>

$(function() {
    $("[name=toggler]").each(function(i) {
        $(this).change(function(){
            $('#blk-1, #blk-2').hide();
            divId = 'blk-' + $(this).val();
            $("#"+divId).show('slow');
        });
    });
 });

The desired toggle effect does not work though. Clicking one radio box fails to hide the other.

Any Ideas?

like image 820
Dooie Avatar asked Jan 12 '11 21:01

Dooie


People also ask

How do you show a div when a radio button is selected?

You can simply use the jQuery show() and hide() methods to show and hide the div elements based on the selection of radio buttons. The div boxes in the following example are hidden by default using the CSS display property which value is set to none .


2 Answers

<label><input id="rdb1" type="radio" name="toggler" value="1" />Money</label>
<label><input id="rdb2" type="radio" name="toggler" value="2" />Interest</label>

<div id="blk-1" class="toHide" style="display:none">
    money
</div>
<div id="blk-2" class="toHide" style="display:none">
    interest
</div>

$(function() {
    $("[name=toggler]").click(function(){
            $('.toHide').hide();
            $("#blk-"+$(this).val()).show('slow');
    });
 });

as in http://www.jsfiddle.net/eKFrW/

like image 91
davin Avatar answered Nov 16 '22 01:11

davin


$("input:radio").click(function(){
    $("div").hide();
    var div = "#blk-"+$(this).val();
    $(div).show();
});

Online demo here: http://jsfiddle.net/yLJPC/

like image 31
amosrivera Avatar answered Nov 16 '22 02:11

amosrivera