Solved

get value for form field

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)

919 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

20 Experts available now in Live!

Get 1:1 Help Now