By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,382 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,382 IT Pros & Developers. It's quick & easy.

Can't get selectedIndex property for listbox

P: n/a
Posting again, since my first attempt never appeared:

In the onClick method of a listbox, I am attempting to retrieve the
selectedIndex property. The listbox is set up to allow multiple selections.
Unfortunately, the property returns -1. Clicking again returns the proper
result. Yet when I do the same thing on the same listbox after changing it
to allow only one entry to be selected, it always returns the proper result
on the first click. This is a pain. Is there any way to get the proper
value during the onClick when the listbox is set to allow multiple entries?

Thanks,

Russell Campbell

Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Russell Campbell wrote:
Posting again, since my first attempt never appeared:

In the onClick method of a listbox, I am attempting to retrieve the
selectedIndex property. The listbox is set up to allow multiple selections.
Unfortunately, the property returns -1. Clicking again returns the proper
result. Yet when I do the same thing on the same listbox after changing it
to allow only one entry to be selected, it always returns the proper result
on the first click. This is a pain. Is there any way to get the proper
value during the onClick when the listbox is set to allow multiple entries?

Thanks,

Russell Campbell


<form name="myForm">
<select multiple onchange="alert(getSelectedIndexes(this));">
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
<option value="1">1</option>
</select>
</form>
<script type="text/javascript">
function getSelectedIndexes(selObj) {
if (selObj.type == 'select-one') {
return new Array(selObj.selectedIndex);
} else {
var indexes = new Array();
for (var i = 0; i < selObj.options.length; i++) {
if (selObj.options[i].selected) {
indexes.push(i);
}
}
return indexes;
}
}
</script>

The function returns an array. Using alert() calls .toString() on the returned
on the array, which returns [theArray].join(","), that's why you get a
comma-delimited list in the alert().

To use the returned array to do something useful, you'd have to iterate through
it.

The other alternative is to have the function return an associative array:

function getSelectedIndexes(selObj) {
var indexes = new Object();
if (selObj.type == 'select-one') {
indexes['idx' + selObj.selectedIndex] = true;
return indexes;
} else {
for (var i = 0; i < selObj.options.length; i++) {
if (selObj.options[i].selected) {
indexes['idx' + i] = true;
}
}
return indexes;
}
}

Now you could test for a particular item being selected directly:

onchange="if (getSelectedIndexes(this)['idx4']) { alert('4th index is
selected');"

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #2

P: n/a
Lee
Russell said:

Posting again, since my first attempt never appeared:

In the onClick method of a listbox, I am attempting to retrieve the
selectedIndex property. The listbox is set up to allow multiple selections.
Unfortunately, the property returns -1. Clicking again returns the proper
result. Yet when I do the same thing on the same listbox after changing it
to allow only one entry to be selected, it always returns the proper result
on the first click. This is a pain. Is there any way to get the proper
value during the onClick when the listbox is set to allow multiple entries?


That's like complaining that you are able to get the birthdate of
an individual, but you're not getting a value when you ask for the
birthdate of a household.

If more than one item can be selected, the concept of a selectedIndex
doesn't really make sense, does it? You loop through the options to
see which have the "selected" attribute set to true.

Jul 20 '05 #3

P: n/a
"Russell Campbell" <no****@byteme.com> writes:
Posting again, since my first attempt never appeared:

In the onClick method of a listbox, I am attempting to retrieve the
selectedIndex property.


You should probably use "onchange" instead. The first click might just
expand the selection box, but haven't selected anything yet.

/l
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.