Solved

get value for form field

Posted on 2009-07-13
4
523 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
  • 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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)

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now