Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Desmond Dallas
6,985 PointsJavascript
Hi in need of help. Im trying to get the values from the radio buttons but keep getting an error. Can someone please explain to me why.
<!DOCTYPE html> <html> <head>
</head>
<style>
function getRadioCheckedValue(radio_name) { var oRadio = document.forms[0].elements[radio_name];
for(var i = 0; i < oRadio.length; i++) { if(oRadio[i].checked) { return oRadio[i].value; } }
return ''; }
</style>
<body> <input type="radio" id="work_abroad_y" name="work_abroad" value="y" /><label for="work_abroad_y">Yes</label>
<input type="radio" id="work_abroad_n" name="work_abroad" value="n" /><label for="work_abroad_n">No</label>
<tr><td colspan="2"><input type="button" name="Submit Data" value="Submit Information" onclick="processFormData(this.form);" ></td></tr>
</body>
</html>
2 Answers
Steven Parker
243,656 PointsI see a couple of issues:
- the code is in a "style" element instead of a "script" element
- the code references "
document.forms" but there are no form element(s)
Desmond Dallas
6,985 PointsOkay Thank you
Desmond Dallas
6,985 PointsDesmond Dallas
6,985 PointsThanks for the reply. Do you have any example that you can show me as I a bit lost on this one
Steven Parker
243,656 PointsSteven Parker
243,656 PointsJust change the
<style>tags to<script>and then put a<form>element around all theinputelements.It's not entirely clear what you intend to do since once the function is defined, there's no code here that uses it. But I did try it in the console to be sure it works.