JQuery if condition for name and value of radio button

I have a type radio button

<input type="radio" name="user-type" value="Store"> <input type="radio" name="user-type" value="Brand"> 

I tried writing a jQuery script as

 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 this was, any correction is evaluated. Thanks.

Update1

I tried this way and it will hide

 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(); } 
+7
javascript jquery
source share
7 answers

Try using the following code - it basically listens for click on the radio name=user-type and switches based on which switch was pressed.

 $(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(); } }); }); 

Working fiddle: http://jsfiddle.net/FpUSH/1/

+5
source share

This is short and it works!

 $('input:radio').change( function(){ if($(this).val() == 'Store'){ $('.showbrand').hide(); $('.showstore').show(); } else{ $('.showstore').hide(); $('.showbrand').show(); } } ); 
+5
source share

You have one syntax error. You have closed parenthesis if the statement is incorrect

 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"); }); 

NEW DEMO

+2
source share

there are only some syntax errors:

 $("input[name='user-type']:checked").each(function(){ if($(this).val() == "Brand"){ $(".showstore").hide(); $(".showbrand").show(); } }); 
+2
source share

You need to check the following:

 if($('input[name="user-type"]:checked').val() === "Brand") { alert("Test"); // do something here } 

Working script example

+2
source share

demo try this

 if(jQuery("input:radio[name='user-type']:checked").val()=="Brand"){ //do your stuff } 
+1
source share

Definitely, @karthikr has a nice and functional approach.

Here is one example for the Yes / No buttons in case you want to disable an item depending on the radio button selection

 $("input[name=button-name]:radio").click(function () { if ($('input[name=input-radio-name]:checked').val() == "Yes") { $( "element" ).removeClass('btn-display-none'); $( "element" ).prop('disabled', false); } else { $( "element" ).addClass('btn-display-none')); $( "element" ).prop('disabled', true); } }); 

Source: used for own project.

0
source share

All Articles