?
Solved

Access a form element by name

Posted on 2003-03-19
7
Medium Priority
?
287 Views
Last Modified: 2013-11-19
I have a script that needs to access a form element by it's name.  I have tried a number of different methods:

document.frmMain["element" + x].value
document.frmMain.["element" + x].value
document.forms[0]["element" + x].value
document.forms[0].["element" + x].value

all of which have not worked.

I have a large number of form elements and do not want to loop through all of them comparing the name.  Is there some way to do this?
0
Comment
Question by:getageek
[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
7 Comments
 
LVL 3

Expert Comment

by:DeAn
ID: 8169510
could you provide code sample please
0
 
LVL 3

Accepted Solution

by:
DeAn earned 225 total points
ID: 8169522
var e = eval("document.frmMain.elementArray[" + x + "]")
alert(e.value)
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8169544
Not sure what you need, but an easy way to access the element is in the element tag.  <input name="test" onChange="someFunction(this);">

function someFunction(e){
    alert(e.value)
}
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 2

Expert Comment

by:mikkolsoft
ID: 8169719
Try
document.frmMain.elements['abc'].value

where abc is the name of your element.

Hope that helps.

Mikkol
0
 
LVL 19

Expert Comment

by:webwoman
ID: 8169849
Or if you want to just cycle through and don't need names...
document.frmMain.elements[i].value
Where i is a counter.

HOWEVER -- this assumes that ALL elements are text elements, not radio buttons, checkboxes, or dropdowns. Those would be accessed differently.

It's MUCH easier to do this server side when the form is completed. What are you trying to do?
0
 

Author Comment

by:getageek
ID: 8170113
Thanks so much!
0
 
LVL 3

Expert Comment

by:DeAn
ID: 8182271
glad to help
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

777 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