Forum Moderators: open
Here's the simplified version of the script.
function Validate()
{
var msg = "";
if (team.value == "" ¦¦ team.value == "Your Team" ) msg += "team\n";
if (!stdnumber[0].checked &&!stdnumber[2].checked ) msg += "standard number\n";
if ( msg!= "" )
{
alert("Forgot to fill out:\n" + msg);
}
else
{
alert("Thank you")
}
}
and here's part of the actual html
3. - Standard number of people?
<BR>
<input type="radio" ID="stdnumber" value="yes">Yes
<br>
<input type="radio" ID="stdnumber" value="no">No
As you can see from the first part, I'm accumulating if statements to tag onto my 1 giant alert box. i've noticed a lot of people using the for...loop but i don't really understand it, and ideally I'd like to stick to the above format. changing the format so that it reads slightly differently also makes my radio buttons stop working (as seen below):
( (stdnumber[0].checked!= true) && (stdnumber[1].chcked!= true) )
I'm still kind of learning JS, but this has really stumped me. The above works perfectly as checkboxes, but I'd like them to be radio buttons.
If anyone could help, that would be GREATLY appreciated! :)
thanks!
A couple things here, probably due to your simplifying, :-) but will point them out nonetheless:
if (team.value == "" ¦¦ team.value == "Your Team" ) msg += "team\n";
if (!stdnumber[0].checked &&!stdnumber[2].checked )
Not seeing where you assign the doc objects to the variable/array team and stdnumber, but assuming you got that ok.
Here's where your validation issues arise. You're going to holler a big "DOH!" :-)
<input type="radio" ID="stdnumber" value="yes">Yes
<input type="radio" ID="stdnumber" value="no">No
ID's need to be unique. But there's a second problem: when you submit this, you need a name parameter to be read by your script. So a good solution for ID'ing radios is to use the value for the ID, or append the value to the name:
<input type="radio" name="stdnumber" id="stdnumber-yes" value="yes"> Yes
<input type="radio" name="stdnumber" id="stdnumber-no" value="no"> No
I'm not sure if I had mentioned it before, but I also am not using the form field as it keeps resetting my form (urgh). that also explains why i dont have name="blah" values in any part of my form.
if all else fails, I'll just turn those radio buttons to checkboxes.
thanks for any help you guys can give me! :D
a) restrict your reference to the object to document.getElementById('objectname') (named objects are deprecated for client-side references,)
var obj = document.getElementById('objectname');
or
b) use the this keyword to pass the specific object,
<input type="checkbox" name="somename" id="somename" value="somevalue" onclick="whatever(this);">
function whatever (obj) { alert(obj.value + ' ' + obj.checked); }
Using your example,
<input type="radio" name="stdnumber" id=stdnumber-yes" value="yes">Yes
<input type="radio" name="stdnumber" id=stdnumber-no" value="no">No
function Validate()
{
var msg = "";
var y = document.getElementById('stdnumber\-yes');
var n = document.getElementById('stdnumber\-no');
if (team.value == "" ¦¦ team.value == "Your Team" ) msg += "team\n";
if (!y.checked &&!n[2].checked) msg += "standard number\n";
if ( msg!= "" )
{
alert("Forgot to fill out:\n" + msg);
}
else
{
alert("Thank you")
}
}
For simplicity's sake (for me..) I'm just going to change all the radio buttons to checkboxes. At least I know those definitely work with my JS.
Thanks again for your help!