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?
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 .
<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/
$("input:radio").click(function(){
$("div").hide();
var div = "#blk-"+$(this).val();
$(div).show();
});
Online demo here: http://jsfiddle.net/yLJPC/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With