Javascript Multiple select menu

K

kiran

hi,
iam creating a multiselect menu in javascript the code for that is as
follows
"<select id=\"groups\" name=\"groups[]\" size=\"4\" multiple>"
"<option value=x>xyz</option>"
..
..
..
..
"</select>"
iam doing this completely in javascript. Its a dynamic menu. So how can
retrieve the values in javascript itself . if anyone knows kindly let
me know
 
P

Paul

kiran said:
hi,
iam creating a multiselect menu in javascript the code for that is as
follows
"<select id=\"groups\" name=\"groups[]\" size=\"4\" multiple>"
"<option value=x>xyz</option>"
.
.
.
.
"</select>"
iam doing this completely in javascript. Its a dynamic menu. So how can
retrieve the values in javascript itself . if anyone knows kindly let
me know
try this:
<script>
var dynamicOpts = new Array(
new Option("xyz", "x"),
new Option("yzx", "y"),
new Option("zxy", "z"),
new Option("foo", "f"),
new Option("bar", "r"),
new Option("baz", "b")
);

function makeSelect(){
var theSelect = window.document.createElement("select");
arguments.callee.theSelect = theSelect;
theSelect.id = "groups";
theSelect.name = "groups[]";
theSelect.size = "4";
theSelect.multiple = "multiple";
var theOptions = theSelect.options;
for(var i=0;i<dynamicOpts.length;i++){
theOptions[theOptions.length] = dynamicOpts;
}
document.getElementById("genContFrm").appendChild(theSelect);
}
function showSelected(){
var opts = makeSelect.theSelect.options;
var selVals = "";
for(var i=0; i<opts.length;i++){
if(opts.selected){
selVals += opts.value + ",";
}
}
alert(selVals);
}
window.onload = makeSelect;

</script>

<form id="genContFrm">
<input type="button" onclick="showSelected()" value="show selected" />
</form>
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top