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

Christian Heilmann codepo8 at gmail.com
Thu Aug 24 09:12:39 CDT 2006


> 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:

PHP does the [] to array conversion for you, JS does not. You'll need
to loop through the options of the select and test which one has the
selected property set to true.

There is an example you can use in the code examples for my book:
function checkMultiple(){
			var f=document.forms[0];
			var selectBox=f.elements['multisubject'];
			var choices=[];
			for(var i=0;i<selectBox.options.length;i++){
				if(selectBox.options[i].selected==1){
				choices.push(selectBox.options[i].text);
				}
			}
			alert(choices.join(','));
		}

http://www.beginningjavascript.com/Chapter7/exampleSelectChoice.html

HTH
Chris

-- 
Chris Heilmann
Book: http://www.beginningjavascript.com
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/



More information about the thelist mailing list