Reputation: 517
I am pulling my hair out. I have a form on my webpage and some javascript that is pulling the values from the form and I am taking various actions based on the input.
When troubleshooting, The value is being returned as undefined.
JS
var days = document.forms["meal-radio"].elements["days"].value;
HTML
<form class= "meal-table" id="meal-radio" onsubmit="return false">
<div class ="meal-plan-choice row" id="Meals4">
...................................................................
<fieldset id="days">
<div class="width40">
<input type="radio" id="radio08" name="days" value="Five"/>
<label for="radio08"><span class="radiospan"></span>5 Days</label>
</div>
<div class="width40">
<input type="radio" id="radio09" name="days" value="Seven"/>
<label for="radio09"><span class="radiospan"></span>7 Days</label>
</div>
</fieldset> <!-- end style fieldset-->
..............................................................
</div></form>
This is not working only in IE, works fine in Chrome and Firefox
Upvotes: 0
Views: 274
Reputation: 783
With pure javascript:
// Get the form
var form = document.getElementById('meal-radio');
// Which is selected?
var currentSelected = form.querySelector('input[name="days"]:checked').value;
// Alert the value
alert( currentSelected ); // temp-alert
jsfiddle:
https://jsfiddle.net/atr46c9r/1/
Do note that in order to get the current selected radio it needs to actually have it's property set to :checked else there will/could be some issues in certain browsers.
Upvotes: 0
Reputation: 21672
Being that you tagged jquery in your question, I'm going to assume you're willing to accept jQuery answers.
You could use this, which works in all browsers:
var days = $("#days input[type='radio']:checked").val()
Upvotes: 2