Home > OS >  how to get "NA" value if radio button is not selected or disabled?
how to get "NA" value if radio button is not selected or disabled?

Time:02-23

I am trying to get value as "NA" if there is no radio button option is selected. Right now i use this code $("input[name='question']:checked").siblings('label').text() but by this code i am getting value as Blank if any radio button is not checked or question is disabled.

CodePudding user response:

Please check the below sample code this might be helpful:

I have used the querySelector DOM method which uses common name porperty of radio buttons inside it.

<label>
    <input type="radio" name="question" value="1"> Question1
</label>
<label>
    <input type="radio" name="question" value="2"> Question2
</label>
  
<script>
    $(document).ready(function(){
  
       var getSelectedValue = document.querySelector( 'input[question="type"]:checked');   
       alert("getSelectedValue>>>" getSelectedValue);
       if(getSelectedValue!= null)
       {
       console.log("radio button selected")
       }
       else{
       console.log("not selected")
       
       //here you can add your logic for 'NA'
       }
});
</script>

CodePudding user response:

I have used this $("input[name='question']:checked").siblings('label').text() || "NA" and it works.Let me know if iit is not a good practice.

  • Related