Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: get radio button value

Tags:

I have the following HTML:

HTML:

<input type="radio" name="abc" value="0" selected="selected" style="display:none" />
<input type="radio" name="abc" value="1" />1+
<input type="radio" name="abc" value="2" />2+
<input type="radio" name="abc" value="3" />3+

JQuery to get the selected radio button

$('input:radio[name=abc]:checked').val();

Why doesn't the code above work on page load, BEFORE a user selected a radio button? It's strange because the code above does work AFTER a user selected a radio button.

It seems to me that I have set the default radio button value to be 0, but if you

Meaning, if the radio button value is selected, return the selected value - otherwise, return 0 (when no value has been selected)

like image 403
StaceyH Avatar asked Nov 09 '10 22:11

StaceyH


People also ask

How can I check if a RadioButton is selected in jQuery?

We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.

How do you find the value of elements of a row of a table when the radio button is selected?

$("selector-for-the-table"). on("click", "input[type=radio]", function() { var row = $(this). closest("tr"); // ... }); If you want to get information from the other elements in that same row, you can use row.


1 Answers

You are using the wrong attribute. For radio buttons you have to use the checked attribute and not the selected attribute.

<input type="radio" checked="checked">  

or just:

<input type="radio" checked>
like image 155
Šime Vidas Avatar answered Sep 28 '22 20:09

Šime Vidas