Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Access a form element by name

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
getageek
Asked:
getageek
1 Solution
 
DeAnCommented:
could you provide code sample please
0
 
DeAnCommented:
var e = eval("document.frmMain.elementArray[" + x + "]")
alert(e.value)
0
 
DeAnCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mikkolsoftCommented:
Try
document.frmMain.elements['abc'].value

where abc is the name of your element.

Hope that helps.

Mikkol
0
 
webwomanCommented:
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
 
getageekAuthor Commented:
Thanks so much!
0
 
DeAnCommented:
glad to help
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now