get value for form field

See sample code below-- note two forms with identically named 'phone number' fields. In JS, I want to get the value for 'phone' from the form that the user submitted. (This is a small example -- I have dozens of fields in the real forms... so I don't want to give every single field in each form its own unique ID.)
<form id='form1'>
   phone number: <input name='phone'>
   <input type='submit' onClick="validate('form1');">
</form>
 
<form id='form2'>
   phone number: <input name='phone'>
   <input type='submit' onClick="validate('form2');">
</form>
 
 
then in the JS...
 
<script>
 
function validate(f) {
   // note that f == 'form1' or 'form2'
   // how do I get the value of the 'phone' field from the form the user submitted?
   // I'm using the Prototype library, if it helps...
}
 
</script>

Open in new window

pnoericAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jonah11Commented:

<form id='form1'>
   phone number: <input name='phone'>
   <input type='submit' onClick="validate('form1');">
</form>
 
<form id='form2'>
   phone number: <input name='phone'>
   <input type='submit' onClick="validate('form2');">
</form>
 
 
then in the JS...
 
<script>
 
function validate(f) {
alert(document.forms[f].phone.value);
}
 
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dvz-Commented:
document.f.phone.value

i believe
0
Jonah11Commented:
dvz, that won't work because he's passing in the string name for the form.
0
dvz-Commented:
jonah, ah thanks for the correction.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Components

From novice to tech pro — start learning today.