Click to See Complete Forum and Search --> : Syntax
andersoni
03-11-2003, 08:34 AM
I have a e-commerce system I am trying to iron out. There is a typicle form that captures the shipping and billing information from the user. There is an option for copying billing info to shipping and I need to perform a check on the required fields for both billing and shipping (if they are not copying info from one to the other). I have two radio buttons on the form (see below) for selecting to copy or not. I am trying to do a conditional statement in javascript to see if the value of the radio button is 'yes' or 'no', then I know if I need to validate fields for shipping or not. Being new to javascript I am having a tough time with the syntax of determining the value of the radio button. The code for the radio buttons are below and what I have so far for the javascript.
Any suggestions?
Thanks
Ian
----Radio buttons -----
<input type="radio" name="BillAddShip" value="Yes" Checked>Yes<br>
<input type="radio" name="BillAddShip" value="No">No
------Javascript-----
if (document.form1.billaddship.value == "No"){
if(trim(document.form1.nameship.value)){
alert("Please input your Shipping Name!");
document.form1.nameship.focus();
return false;
}
}
gil davis
03-11-2003, 08:58 AM
1) Correct capitalization is essential. Your form names are "BillAddShip", therefore the syntax would bedocument.form1.BillAddShip
2) When you use the same name for form elements, you create an array. Therefore you havedocument.form1.BillAddShip[n]where "n" is 0 or 1. That also means that there is no ".value" property for document.form1.BillAddShip, because it is an array.
3) Unlike the normal form submission, you do not get the value automatically from the radio button in javascript. You have to test the ".checked" property and act accordingly. This code will give you the value of the checked box:var ans = (document.form1.BillAddShip[0].checked) ? document.form1.BillAddShip[0].value : (document.form1.BillAddShip[1].checked) ? document.form1.BillAddShip[1].value : "";
If you don't need the value for server-side processing, don't give them two buttons - just give them one. It will be easier for you in the long run.
andersoni
03-11-2003, 10:10 AM
Thanks for the quick response.
I have tried making sure the case was correct on BillAddShip, but since my syntax was not correct, I was unsure if case was an issue.
Customer likes the two radio buttons and wants them, I agree a simple checkbox would be better.
Under number 3, I had thought that the radio buttons might be looked at as an array, again uncertain of syntax. Concerning the code you gave me, I assume the 'yes' / 'no' value is placed in 'var ans' and I then check it in
- if (var ans == 'No'){ ... ?
Ian
gil davis
03-11-2003, 10:17 AM
I assume the 'yes' / 'no' value is placed in 'var ans'Correct.
It can be further simplified by ignoring the "No" button, and just looking to see if document.form1.BillAddShip[0].checked is true. Then it doesn't matter what the value is.
andersoni
03-11-2003, 10:39 AM
I see what you are saying, just check one button for checked or unchecked and forget the actual value. Makes sense.
I will install it and let you know how it turns out.
Thanks
Ian
andersoni
03-11-2003, 07:22 PM
What does document.form1.BillAddShip[0].checked return? True/False, Yes/No, 1/0, -1/0. I got your first code to work but would like to shorten it up and just test to see if the first radio button is selected.
Ian
gil davis
03-12-2003, 06:01 AM
It is a boolean variable. It either returns true or false.