Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In jQuery, how do I get the value of a radio button when they all have the same name?

Here is my code:

<table>    <tr>       <td>Sales Promotion</td>       <td><input type="radio" name="q12_3" value="1">1</td>       <td><input type="radio" name="q12_3" value="2">2</td>       <td><input type="radio" name="q12_3" value="3">3</td>       <td><input type="radio" name="q12_3" value="4">4</td>       <td><input type="radio" name="q12_3" value="5">5</td>    </tr> </table> <button id="submit">submit</button> 

Here is JS:

$(function(){     $("#submit").click(function(){               alert($('input[name=q12_3]').val());     });  }); 

Here is JSFIDDLE! Every time I click button it returns 1. Why? Can anyone help me?

like image 732
SPG Avatar asked Aug 04 '13 13:08

SPG


People also ask

How do you find the value of a radio button group?

To get the value of selected radio button, a user-defined function can be created that gets all the radio buttons with the name attribute and finds the radio button selected using the checked property. The checked property returns True if the radio button is selected and False otherwise.

Can radio button have same value?

The only thing you have to change in your code is the name of the radio button because all radio buttons with the same name are assumed to be a group, and the web-browser only allows selection of only one of them by default, and you cannot control that.

Which method can be used to obtain the value associated with the selected radio button?

Get the value of selected radio button: querySelector() The querySelector() function is a DOM method of JavaScript. This method is used to get the element that matches with the specified CSS selector in the document.


2 Answers

In your code, jQuery just looks for the first instance of an input with name q12_3, which in this case has a value of 1. You want an input with name q12_3 that is :checked.

$("#submit").click(() => {    const val = $('input[name=q12_3]:checked').val();    alert(val);  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>    <table>    <tr>      <td>Sales Promotion</td>      <td><input type="radio" name="q12_3" value="1">1</td>      <td><input type="radio" name="q12_3" value="2">2</td>      <td><input type="radio" name="q12_3" value="3">3</td>      <td><input type="radio" name="q12_3" value="4">4</td>      <td><input type="radio" name="q12_3" value="5">5</td>    </tr>  </table>  <button id="submit">submit</button>

Note that the above code is not the same as using .is(":checked"). jQuery's is() function returns a boolean (true or false) and not (an) element(s).


Because this answer keeps getting a lot of attention, I'll also include a vanilla JavaScript snippet.

document.querySelector("#submit").addEventListener("click", () => {    const val = document.querySelector("input[name=q12_3]:checked").value;    alert(val);  });
<table>    <tr>      <td>Sales Promotion</td>      <td><input type="radio" name="q12_3" value="1">1</td>      <td><input type="radio" name="q12_3" value="2">2</td>      <td><input type="radio" name="q12_3" value="3">3</td>      <td><input type="radio" name="q12_3" value="4">4</td>      <td><input type="radio" name="q12_3" value="5">5</td>    </tr>  </table>  <button id="submit">submit</button>
like image 196
Bram Vanroy Avatar answered Oct 24 '22 15:10

Bram Vanroy


in your selector, you should also specify that you want the checked radiobutton:

$(function(){     $("#submit").click(function(){               alert($('input[name=q12_3]:checked').val());     });  }); 
like image 23
Dennis Avatar answered Oct 24 '22 16:10

Dennis