I have 2 radio buttons div structure below, I need to enable only one of them and also with selected radio button i ned to enable div’s
for ex: if input-cncstoreloc need to show ‘div1’ and need to hide ‘div2’(if div is in show mode) and if input-shipstoreloc is selected ‘div2’ should be enable need to hide ‘div2’(if div is in show mode)
Below are the custom css3 radio buttons(input type is hidden) and while selection i am adding one class radio_chk to show it is checked in span tag
Tried below but not sure how to show the divs
$('input[name="cncstoreloc"]').on('click', function(){
$('input[name="cncstoreloc"]').find("span").removeClass('radio_chk');
$(this).find("span").addClass('radio_chk');
});
The div’s not in the input they are at some other place in the page need to control them by these radio buttons. and also the div’s have different classes and id’s.
[quote=“koder, post:3, topic:230401, full:true”]
The div’s not in the input they are at some other place in the page need to control them by these radio buttons. and also the div’s have different classes and id’s.[/quote]
Good one. First up you’ll want to hide those divs when the page loads. We can use a data-details attribute on each radio button so that we can loop through them and hide those divs. A nice thing about this is that when the radio button changes, we can easily update the appropriate divs.
$("[name='cncstoreloc']").each(function () {
var id = $(this).data("details");
$("#" + id).hide();
});
The appropriate divs are now hidden. When the radio button is changed, if the radio button is currently selected we want to show it’s appropriate div, and otherwise we want to hide it. This can be quite easily done with:
$("[name='cncstoreloc']").on("change", function () {
var id = $(this).data("details");
if ($(this).is(":checked")) {
$("#" + id).show();
} else {
$("#" + id).hide();
}
});
Putting it all together we get:
$("[name='cncstoreloc']").each(function () {
var id = $(this).data("details");
$("#" + id).hide();
})
.on("change", function () {
var id = $(this).data("details");
if ($(this).is(":checked")) {
$("#" + id).show();
} else {
$("#" + id).hide();
}
});