Noob Problem, Changing Checkbox[dyn_name][i] State

F

Frobinrobin

Hi all,

I'm using the following checkbox naming convention the reason being
that when I POST the form, PHP read it as a multidimonsional array
fairly easily.

<input type="checkbox" name="enabled[parent_name][]">

so when I post the form I will see (from PHP):

[enabled] => Array
(
[BaseMenu] => Array
(
[0] => 2
)

[Menu1] => Array
(
[0] => 40
[1] => 41
[2] => 42
)
[SubMenu1] => Array
(
[0] => 22
[1] => 21
)
etc...etc.....


This is an excellent way for POST'ing to PHP, the problem I have is at
the Client Side... when I deselect a 'master' checkbox (i.e. Menu1) I
want the sub menu's to deselect (i.e. Submenu1)

This doesnt happen, I've got my onClick working to the point that I
can display the various passed arguments.
So when I click on Menu1 it will count the number of checkboxes for
SubMenu1 and display an alert dialog whilst incrementing on each loop
but this only outputs a string.

So why does the following not work?
document.form.enabled[Submenu1].checked = false;

I cannot even use ...
alert(document.form.enabled[Submenu1].value)

Obviously it is the way I am accessing the checkboxes - can these be
mutli-dimensional?
checkboxname[group_name][0], checkboxname[group_name][1]
 
F

Frobinrobin

So why does the following not work?
document.form.enabled[Submenu1].checked = false;


Basically, because when using dot notation you can't use square
brackets (since they are another, more flexible but more verbose, way
of referencing a property.

Take a look athttp://www.dev-archive.net/articles/js-dot-notation/index.html
it should help you construct some syntax that matches the elements you
actually have.


excellent and thanks.. however I still cant get it right!
this is the (newly updated) code that takes arguments parent_name
(i.e. Menu1) and should change the value but I'm getting no errors.

// this next line works fine:
var num_parents = document.form.elements["enabled[" + parent_name + "]
[]"].length;

for (var i = 0; i < num_parents; i++) {

if(form["enabled[" + parent_name + "][" + i + "]"] == true)
{
form["enabled[" + parent_name + "][" + i + "]"] = false;
}
else
{
form["enabled[" + parent_name + "][" + i + "]"] = true;
}
}

I realise that I havent added the "checked" property.. I dont
understand how to use it with square brackets.. any help would be much
appreciated :)
 
L

Lee

Frobinrobin said:
Hi all,

I'm using the following checkbox naming convention the reason being
that when I POST the form, PHP read it as a multidimonsional array
fairly easily.

Forgive me for not bothering to read further.

This newsgroup, like most technical (at least) newsgroups
has a FAQ, and pointers to it are posted regularly.

http://www.jibbering.com/faq/#FAQ4_25


--
 
F

Frobinrobin

Frobinrobin said:





Forgive me for not bothering to read further.

This newsgroup, like most technical (at least) newsgroups
has a FAQ, and pointers to it are posted regularly.

http://www.jibbering.com/faq/#FAQ4_25

--

I just cant get my head around this as I use PHP primarily and just
want the checkboxes to act this way and be named this way... from what
Im reading you are saying that Javascript can't read it correctly? and
that [] characters should never be used for inputs? Please correct me
if I'm wrong as I like using them!

So for this form, I bite the bullet and rename the checkboxes to
"enabled_GroupName_i+" and use PHP to split the name up. Seems like
the easiest way since I've been at this for hours!
 
G

Gregor Kofler

Frobinrobin meinte:
This newsgroup, like most technical (at least) newsgroups
has a FAQ, and pointers to it are posted regularly.

http://www.jibbering.com/faq/#FAQ4_25

--

I just cant get my head around this as I use PHP primarily and just
want the checkboxes to act this way and be named this way... from what
Im reading you are saying that Javascript can't read it correctly? and
that [] characters should never be used for inputs?

No, but JS doesn't care about square brackets in attribute values.
The name of the element is "enabled[foo][bar]". In your case
document.getElementsByName("enabled[Menu][]") will return a collection
of the input nodes with precisely that name.

Gregor
 
F

Frobinrobin

Frobinrobin meinte:
I just cant get my head around this as I use PHP primarily and just
want the checkboxes to act this way and be named this way... from what
Im reading you are saying that Javascript can't read it correctly? and
that [] characters should never be used for inputs?

No, but JS doesn't care about square brackets in attribute values.
The name of the element is "enabled[foo][bar]". In your case
document.getElementsByName("enabled[Menu][]") will return a collection
of the input nodes with precisely that name.

Gregor

--http://www.gregorkofler.at::: Landschafts- und Reisefotografiehttp://www.licht-blick.at ::: Forum für Multivisionsvorträgehttp://www.image2d..com ::: Bildagentur für den alpinen Raum

Absolutely rocking!! Once I realised I needed to use the 'object'
variable I assigned to the getElementByName (rather than trying to use
document.getElementsByName("enabled["+id+"]["+i+"]") directly) and now
it's all go!

Thanks guys :)
 

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
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top