[thelist] How can I pass a HTML "array" over to Javascript

Robert Vreeland vreeland at studioframework.com
Thu Aug 24 09:57:17 CDT 2006


I think I see your propblem - how to loop through a form field where the
name is in an array syntac (layers[] rather then layer). One method is to
include a reference to the form in your onsubmit event handler using the
'this' keyword; for example:

	<form ... Onsubmit="return myValidator(this);">

Then in your event handler you can examine each field in turn and decide how
to proceed. Remember, javascript sees checkboxs, radio buttons  with the
same name as an array. Knowing this we can build the validator like so:

 function  myValidator(theForm){
	// loop through the form elements
	for(i = 0; i < theForm.length; i++) {
		//.... Validate other input items by name or type
		if(theForm[i].name == "layers[]") {
			//loop through the layers[] check boxes / radio
button
			lyr = theForm[i];	//assign a variable name to
make it easier to work with
			for(lyi = 0; lyi < lyr.length; lyi++) {
				if(lyr[i].value == "cities") {
				//... eliminate that layer
					if(lyr[i].checked) { // if checked
will eval as true
					{
						lyr[i].checked = false;
						// now when the form is sent
to the server this field
						// will not be included
					}
				}
                     }
		}
	}
}

Sincerely,
Robert Vreeland
Managing Partner 
StudioFramework 

-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Stefan Schwarzer
Sent: Thursday, August 24, 2006 9:41 AM
To: thelist at lists.evolt.org
Subject: [thelist] How can I pass a HTML "array" over to Javascript

Hi there,

I have a kind of HTML array ("layer[]"), which appears a couple of times on
my form. It is used so that a user can select multiple layers to be
displayed on a map. So, this would be "cities", "national boundaries",
"lakes", "rivers", etc.

It works fine. No problem with that.

Now, I would like, before passing the elements over to PHP, to do a quick
check with Javascript which layers have been selected, in order to eliminate
eventually one or to add another.

My function to do this looks like this:

                 function change_level()
                 {
                     lyr = new Array();
                     lyr = document.mapserv.layer;
                     for (i=0; i<lyr.length; i++)
                     {
			if (lyr[i] = "cities")
				... eliminate that layer
                     }
                 }

So, here I have a (actually two) problems. One is that I have no clue how
the HTML element "layer" can be passed over to a Javascript- variable. And
second I don't know how I can eliminate one element of that array.

There probably would be some possibility to get the checking done in PHP
too, but the whole process behind the site is somewhat complex.  
So it would come in very handy, if this would be achievable through
Javascript.

Can anyone help me out?

Thanks a lot!!

Stefan
-- 

* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester and archives
of thelist go to: http://lists.evolt.org Workers of the Web, evolt ! 




More information about the thelist mailing list