Solved

get value for form field

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

809 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