The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Checkbox Array with Explicit Indicies
Discuss Checkbox Array with Explicit Indicies in the JavaScript Development forum on Dev Shed. Checkbox Array with Explicit Indicies JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 21st, 2013, 04:36 PM
|
|
|
|
Checkbox Array with Explicit Indicies
I have an array of checkboxes that I need to access via javascript. Generally I can access them like this:
Code:
function sendall(form) {
with (document.main)
alert(send.length);
}
When the alert is executed I get an error saying 'send' is not defined. All the examples of creating checkbox arrays do not use explicit indices (these are in a form named 'main').
Code:
<input type="checkbox" name="send" />
<input type="checkbox" name="send" />
<input type="checkbox" name="send" />
However, I need the checkboxes to properly correspond to other items when processed by PHP. Therefore I am required to explicitly define the indices (this is actually done by PHP in a loop):
Code:
<input type="checkbox" name="send[0]" />
<input type="checkbox" name="send[1]" />
<input type="checkbox" name="send[2]" />
I am guessing that is the reason Javascript thinks the array is undefined (yet $_POST in PHP has it as an array). If my guess is wrong, why am I getting an undefined error? Is there a different HTML syntax for specifying the name with an index? If I am right, how do I get Javascript to recognize the checkboxes as an array? TIA.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

January 21st, 2013, 04:51 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
There's some weird contradictions in your post but I think the reality is that you have in your HTML the "send[X]"-formed elements and you're trying to use document.main.send?
The elements are actually named "send[X]". Javascript/the DOM doesn't know about the special PHP array syntax you can use to name elements so that you get an array in $_POST. You have to get to them with those exact names
Code:
document.main["send[0]"]
or go through document.main.elements looking for ones with the right name.
Code:
with (document.main.elements) {
for (var i = 0; i < length; i++) {
if (/^send\[[0-9]*\]$/.test(item(i).name)) {
// ...
}
}
}
|

January 21st, 2013, 04:58 PM
|
|
|
Ouch! That really complicates things. I guess I need to process them until I get an undefined condition to know how many there are? Assuming, of course, I can programmatically reference them.
Code:
var i=0;
while (document.main["send["+i+"]"]!=undefined) {
.
.
.
i++;
}
P.S. I don't know what contradictions you found but it is more likely that I was not clear enough in my explanation.
Last edited by gw1500se : January 21st, 2013 at 05:12 PM.
|

January 21st, 2013, 05:33 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Well okay, maybe not "contradiction". I thought you were saying that you had the first bit of HTML in place but that didn't jive with the rest of your post. Then you were talking about how you needed it for arrays but obviously it's not possible with that code. So yeah, just a bit confused that's all.
Anyways yes, you can get to them programmatically. By default objects can be treated like arrays so foo.bar and foo["bar"] are equivalent.
Also, document.main.elements.length is your friend.
|

January 21st, 2013, 08:44 PM
|
|
|
|
Got it! Thanks.
|

January 22nd, 2013, 08:14 AM
|
|
|
|
I guess I don't "got it". I thought I understood what you were saying but obviously not as I can't get it to work. So how do I reference elements of the array programmaticaly? Everything I tried still comes back undefined. TIA.
|

January 22nd, 2013, 08:30 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
why not simply assign the elements a class and use that to reference them? That should be the cleanest and most efficient solution.
You should also get rid of the "with" statement.
|

January 22nd, 2013, 08:35 AM
|
|
|
|
Thanks for the reply. To answer your question, I guess because I don't know how to do that either.
Yes, the 'with' was removed some time ago.
|

January 22nd, 2013, 12:55 PM
|
|
|
I finally figured it out. I don't know why but creating the index in a separate variable worked where an embedded string did not.
Code:
var i=0;
var box="send["+i+"]";
while (document.main[box]!=undefined) {
.
.
.
i++;
box="send["+i+"]";
}
I don't know why that is logically different than what I posted earlier.
Code:
while (document.main["send["+i+"]"]!=undefined) {
Last edited by gw1500se : January 22nd, 2013 at 12:58 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|