/    Sign up×
Community /Pin to ProfileBookmark

Get value from array element of a form

Having a form like this:

[code=html]<form name=”imageform”>
<input id=”image1″ type=”file” name=”file[]” value=”” />
<input id=”image2″ type=”file” name=”file[]” value=”” />

<input id=”image10″ type=”file” name=”file[]” value=”” />
</form>[/code]

when I capture the dispatch of the form and try to get the value of any of these file fields:

[code=php]var form = document.forms[‘imageform’];

for (var i = 0; i < form.file.length; i++) {
if (form.file.value != ”) {
// check valid type of file ….
}
}[/code]

There’s a Javascript error saying that form.file is undefined. It’s strange because I see in Firebug that “file” actually exists as a property of “form”. The same happens if I get rid of the temporary variable “form” and I do it this way:

document.forms[‘imageform’].file
or
document.forms.imageform.file

So, I eventually had to use the getElementById(‘image’ + i) method in order to do what I wanted. But there must be a “natural” way to get a value from an array defined in a form.

Any help is welcome.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorSep 22.2011 — The generic reference is [B]elements[/B]. Or you may simply use again square brackets;
<i>
</i>document.forms['imageform'].elements['file[]']

[COLOR="DarkGreen"]//or[/COLOR]

document.forms['imageform']['file[]']

[COLOR="DarkGreen"]// or even[/COLOR]

document['imageform']['file[]']

[COLOR="DarkGreen"]// or, the way your code works:[/COLOR]

var form = document.forms['imageform'];
for (var i = 0; i &lt; form[COLOR="Blue"]['file[]'][/COLOR].length; i++) {
if (form[COLOR="Blue"]['file[]'][/COLOR][COLOR="Red"][i][/COLOR].value != '') {
// check valid type of file ....
}
}

There is also a straight DOM reference like:
<i>
</i>document.getElementsByName('file[]')

In JavaScript the square brackets [I]in a string[/I], even it is about a value of a [B]name[/B] attribute, have no special meaning. Because the value of the HTML attributes are [I]strings[/I]. It is not like in PHP.

Note: The above should be used only if those elements have a [B]name[/B], not an [B]id[/B]. Don't confound the [B]name[/B] attribute with the [B]id[/B] attribute. Different "animals", different tokens, different methods. There's is a major difference: [B]id[/B] must be [I]unique[/I].
Copy linkTweet thisAlerts:
@JazztronikauthorSep 22.2011 — Hello Kor, thanks for clearing things up. I will test that in my code.

Yes, I know the difference between the id and name attributes. What I was struggling with was how to handle this array created from a form, and its values.

Cheers! ?
×

Success!

Help @Jazztronik spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...