jQuery if condition for radio button name and value
Try the following code - it basically listens for click
on radio name=user-type
, and toggles based on which radio button was clicked.
$(function () {
$('.showstore').hide();
$('.showbrand').hide();
$("input[name=user-type]:radio").click(function () {
if ($('input[name=user-type]:checked').val() == "Brand") {
$('.showstore').hide();
$('.showbrand').show();
} else if ($('input[name=user-type]:checked').val() == "Store") {
$('.showstore').show();
$('.showbrand').hide();
}
});
});
A working fiddle: http://jsfiddle.net/FpUSH/1/
This is short
$('input:radio').change(
function(){
if($(this).val() == 'Store'){
$('.showbrand').hide();
$('.showstore').show();
}
else{
$('.showstore').hide();
$('.showbrand').show();
}
}
);
You have one syntax error, You have closed the parenthesis
belongs to the if statement wrongly
if($("input[name=user-type]:checked").val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
DEMO
$("input[name=user-type]").change(function(){
$(".showstore").toggle(this.value === "Store");
$(".showbrand").toggle(this.value === "Brand");
});