I have a radio button like
<input type="radio" name="user-type" value="Store">
<input type="radio" name="user-type" value="Brand">
I tried writing jQuery script like
if($("input[name=user-type]:checked").val()) == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
and also tried
if($("input[name=user-type]:checked").val()) == "Brand").click(function(){
$(".showstore").hide();
$(".showbrand").show();
});
and also tried
if ( $("input[name=user-type]:radio").val() == "Brand"){
$(".showstore").hide();
$(".showbrand").show();
}
None of these worked, any correction is appreciated. Thanks.
Update1
I tried in this way and this hide both
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();
}
To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.
Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.
$('#rdSelect:checked'). val() method returns "on" when radio button is checked and "undefined", when radio button is unchecked. var isChecked = $('#rdSelect').is(':checked'); The above method uses "is" selector and it returns true and false based on radio button status.
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();
}
}
);
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