javascript and []

G

Guest

<form name=f>
<input type=text name="apple[]" value=1>
</form>

<script>
document.f.apple[].value=2
</script>

///////////////////////////
this generates an script error in IE
because of apple[]
so how do I get around the error?
(must use "apple[]" for server cgi)

thanks in advance :)
 
E

Evertjan.

wrote on 11 jul 2004 in comp.lang.javascript:
<form name=f>
<input type=text name="apple[]" value=1>
</form>

<script>
document.f.apple[].value=2
</script>

<form name=f>
<input type=text name="apple[]" value=1>
</form>

<script type="text/javascript">
document.f[0].value=2 // the first child of f
</script>

========= or ==========

<form name=f>
<input type=text name="apple[]" id=appl value=1>
</form>

<script type="text/javascript">
document.getElementById('appl').value=2
</script>
 
J

Janwillem Borleffs

so how do I get around the error?

document.forms['f'].elements['apple[]'].value = 2;
(must use "apple[]" for server cgi)

Not really. I assume that you are using php, which also supports an
always_populate_raw_post_data directive.

With this directive enbled, you can simply use normal names and still use
arrays:

<form method="post" action="...">
<input type="checkbox" name="fruit" value="apple" /> Apple<br />
<input type="checkbox" name="fruit" value="banana" /> Banana<br />
<input type="checkbox" name="fruit" value="pear" /> Pear<br />
<input type="submit" />
</form>
.....
<?
if (!isset($HTTP_RAW_POST_DATA) ||
!strstr($HTTP_RAW_POST_DATA, 'fruit=')) {
print "Please select some fruits";
} else {
parse_str(
str_replace(
"fruit=",
"fruit[]=",
$HTTP_RAW_POST_DATA
),
$fruits
);
print "Selected fruits:<br />";
foreach ($fruits['fruit'] as $fruit) {
print "$fruit<br />";
}
}
?>


JW
 
G

Grant Wagner

Evertjan. said:
wrote on 11 jul 2004 in comp.lang.javascript:
<form name=f>
<input type=text name="apple[]" value=1>
</form>

<script>
document.f.apple[].value=2
</script>

<form name=f>
<input type=text name="apple[]" value=1>
</form>

<script type="text/javascript">
document.f[0].value=2 // the first child of f
</script>

========= or ==========

<form name=f>
<input type=text name="apple[]" id=appl value=1>
</form>

<script type="text/javascript">
document.getElementById('appl').value=2
</script>

document.forms['yourForm'].elements['apple[]'].value = 2;

Now you don't need to remember form input indices or use a
browser that is completely W3C compliant.

This is covered at <url: http://jibbering.com/faq/#FAQ4_25 />.
 
P

Phil Powell

Janwillem Borleffs said:
so how do I get around the error?

document.forms['f'].elements['apple[]'].value = 2;
(must use "apple[]" for server cgi)

Not really. I assume that you are using php, which also supports an
always_populate_raw_post_data directive.

With this directive enbled, you can simply use normal names and still use
arrays:

[snip]
That would depend on if the compilation of your version of PHP
supports ALWAYS_POPULATE_RAW_POST_DATA.. you're safer using an array
construct, sorry.

<input type="checkbox" name="fruit[]" value="apple">Apple

in Javascript you would call this element cross-browser:
document.forms['myForm'].elements['fruit[]']

Phil
<form method="post" action="...">
<input type="checkbox" name="fruit" value="apple" /> Apple<br />
<input type="checkbox" name="fruit" value="banana" /> Banana<br />
<input type="checkbox" name="fruit" value="pear" /> Pear<br />
<input type="submit" />
</form>
....
<?
if (!isset($HTTP_RAW_POST_DATA) ||
!strstr($HTTP_RAW_POST_DATA, 'fruit=')) {
print "Please select some fruits";
} else {
parse_str(
str_replace(
"fruit=",
"fruit[]=",
$HTTP_RAW_POST_DATA
),
$fruits
);
print "Selected fruits:<br />";
foreach ($fruits['fruit'] as $fruit) {
print "$fruit<br />";
}
}
?>


JW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top