Multi select box, post all values

Hello I have a multi-select box. And when I select multiple options, I would be able to post all the selected options. I can do that in the HTML way. But I am unaware of the method using javascript to dynamically build a multiple select box and post all the selected options.

Attached in the code has two parts, one would show how to post all selected values, and the other part would show where I am stuck right now.

In part one, I have the name of my multi select box as an array, so on submit, all selected values are posted. In part two,  I am creating a multi select box dynamically, and if I try to add the name as an array, I get a syntax error point towards the square brackets. Do we have a way around for this problem??
part one:

<form method="post" action="">
    <select name=components[] multiple size=5>
      <option value=1>1</option>
      <option value=2>2</option>
      <option value=3>3</option>
      <option value=4>4</option>
      <option value=5>5</option>
    </select>
<input type="submit" name="submit" value="us" />
</form>


part two:

newSelectorElm = document.createElement('select');
newSelectorElm.id = wName; 
newSelectorElm.name = wName;
//newSelectorElm.name = wName[];
newSelectorElm.multiple = true;

Open in new window

gautam_reddycAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>hielo</title>
<script language="JavaScript" type="text/javascript">

function makeSelect(wName,opts,target)
{
	var newSelectorElm = document.createElement('select');
	newSelectorElm.id = wName; 
	newSelectorElm.name = wName;
	newSelectorElm.multiple ="multiple";

	for( var i in opts)
	{
		newSelectorElm.options[newSelectorElm.options.length]=new Option(opts[i],i);
	}
	document.getElementById(target).appendChild(newSelectorElm);
}

window.onload=function(){
	var opts=['baseball','basquetball','football'];
	makeSelect('sport[]',opts,'targetDiv');
};

</script>

	</head>
	<body>
		<div id='targetDiv'></div>
	</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gautam_reddycAuthor Commented:
Thanks! got my solution!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.