homepage Welcome to WebmasterWorld Guest from 54.166.128.254
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Accessing fieldnames that are an array
figment88

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 5594 posted 4:27 pm on Jun 20, 2006 (gmt 0)

I'm generating a form from a database which makes it so I do not know how many elements there are going to be before hand. To keep track of the data, I am using an array such as

<select name="widgets[]">
<select name="widgets[]">
<select name="widgets[]">

This works great to process the form submit since I get an Array I can use in PHP.

I also want to do some client-side validation, but am having a hard time accessing the widgets array in javascript. For example, document.MyForm.widgets[1].value yields an error.

Since these are the only selects in the form, I can use getElementsByTagName to collect the elements such as
var widgets=document.getElementsByTagName('select');

This though seems like a bad kludge. If I add another select to the form or even a different form on the page, I will create all sorts of problems.

Any ideas on how to access the widget array directly?

 

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5594 posted 4:37 pm on Jun 20, 2006 (gmt 0)

There are some tips on the PHP and HTML [php.net] manual page.

figment88

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 5594 posted 4:54 pm on Jun 20, 2006 (gmt 0)

Thanks for the pointer, I found wehat I needed. Turns out the proper notation is

document.MyForm.elements['widgets[]']

So, I can access values like

document.MyForm.elements['widgets[]'][1].value

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved