Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery if condition for radio button name and value

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();
}
like image 587
Karthik Malla Avatar asked Jun 22 '14 07:06

Karthik Malla


People also ask

How can I know which radio button is selected via jQuery?

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.

How can I check if a radio button is 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.

How check radio box is checked or not in jQuery?

$('#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.


2 Answers

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/

like image 102
karthikr Avatar answered Sep 28 '22 12:09

karthikr


This is short

$('input:radio').change(
function(){
    if($(this).val() == 'Store'){
        $('.showbrand').hide();
        $('.showstore').show();
    }
    else{
        $('.showstore').hide();
        $('.showbrand').show();
    }
}
);  
like image 34
ebiv Avatar answered Sep 28 '22 13:09

ebiv