Solved

get value for form field

Posted on 2009-07-13
4
557 Views
Last Modified: 2013-11-18
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

0
Comment
Question by:pnoeric
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Jonah11 earned 500 total points
ID: 24845961

<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
 
LVL 5

Expert Comment

by:dvz-
ID: 24845962
document.f.phone.value

i believe
0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24845987
dvz, that won't work because he's passing in the string name for the form.
0
 
LVL 5

Expert Comment

by:dvz-
ID: 24846065
jonah, ah thanks for the correction.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question