Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / August 2007



Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frobinrobin - 31 Aug 2007 17:15 GMT
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][i].checked = false;

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

Obviously it is the way I am accessing the checkboxes - can these be
mutli-dimensional?
checkboxname[group_name][0], checkboxname[group_name][1]
David Dorward - 31 Aug 2007 17:23 GMT
> So why does the following not work?
> document.form.enabled[Submenu1][i].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 at http://www.dev-archive.net/articles/js-dot-notation/index.html
it should help you construct some syntax that matches the elements you
actually have.

--
David Dorward
http://dorward.me.uk/
http://blog.dorward.me.uk/
Frobinrobin - 31 Aug 2007 17:37 GMT
> > So why does the following not work?
> > document.form.enabled[Submenu1][i].checked = false;
[quoted text clipped - 9 lines]
> --
> David Dorwardhttp://dorward.me.uk/http://blog.dorward.me.uk/

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 :)
Lee - 31 Aug 2007 17:46 GMT
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

--
Frobinrobin - 31 Aug 2007 18:07 GMT
> Frobinrobin said:
>
[quoted text clipped - 12 lines]
>
> --

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!
Gregor Kofler - 31 Aug 2007 19:05 GMT
Frobinrobin meinte:

>> This newsgroup, like most technical (at least) newsgroups
>> has a FAQ, and pointers to it are posted regularly.
[quoted text clipped - 7 lines]
> 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

Signature

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

Frobinrobin - 31 Aug 2007 19:50 GMT
> Frobinrobin meinte:
>
[quoted text clipped - 18 lines]
>
> --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 :)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.