Solved

get value for form field

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

733 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