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)
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.
$("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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With