Entry
can we select more than one text of listbox through javascript with a single click over the listbox
Apr 6th, 2008 19:30
ha mo, Russ Locke, Colin Fraser, Shyam Vadlamani,
The old answer was:
You cannot do this in JavaScript, nor can you do it in any language I
have ever used.
The click activates a selection at the position of the mouse pointer,
that is all. You can make conditional selections, but they must be
outside the listbox you are trying to select in the first place. There
are a number of examples in FAQTS on how to do this.
However I'll guess you are looking for some like this: (it's not
perfect - but that's what you get for 20 minutes of free work)
<html>
<head>
<script>
function getSelectedIndices(select) {
var r = new Array();
for (var i = 0; i < select.options.length; i++)
if (select.options[i].selected)
r[r.length] = i;
return r;
}
var lastSelectedStates = new Array();
function selectIndices(select) {
var more = -1;
var start = -1;
if (lastSelectedStates.length == 0) {
start = parseInt(getSelectedIndices(select));
}
else {
var newSelectedStates = new Array();
newSelectedStates.length = select.options.length;
for (var i = 0; i < select.options.length; i++) {
if (select.options[i].selected) newSelectedStates[i] = 1;
else newSelectedStates[i] = 0;
}
for (var j = 0; j < newSelectedStates.length; j++) {
if (lastSelectedStates[j] == 0 && newSelectedStates[j] ==
1) {
start = j;
break;
}
}
}
if (start > -1) {
if (select.options[start].value.substring(0,6) == "~more=") {
more = select.options[start].value.substring
(6,select.options[start].value.length);
}
}
if (more > -1) {
for (var s = 0; s <= more; s++) {
if (select.options[s+start])
select.options[s+start].selected = true;
}
}
lastSelectedStates.length = select.options.length;
for (var z = 0; z < select.options.length; z++) {
if (select.options[z].selected) lastSelectedStates[z] = 1;
else lastSelectedStates[z] = 0;
}
}
</script>
</head>
<body>
<form>
<select multiple size=30 onchange="selectIndices(this)">
<option value="~more=4">colors (all)
<option value="red">red
<option value="blue">blue
<option value="green">green
<option value="yellow">yellow
<option value="~more=5">shapes (all)
<option value="circle">cirlce
<option value="square">square
<option value="rectangle">rectangle
<option value="triangle">triangle
<option value="oval">oval
<option value="~more=6">locations (all)
<option value="here">here
<option value="there">there
<option value="everywhere">everywhere
<option value="somewhere">somewhere
<option value="no where">no where
<option value="not here">not here
<option value="~more=3">tastes (all)
<option value="bland">bland
<option value="mild">mild
<option value="spicy">spicy
<option value="~more=9">to (all)
<option value="Sheila">Sheila
<option value="Ike">Ike
<option value="Tom">Tom
<option value="Kim">Kim
<option value="Russ">Russ
<option value="Jamie">Jamie
<option value="Kilroy">Kilroy
<option value="Henry">Henry
<option value="Sara">Sara
</select>
</form>
</body>
</html>
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com