473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can't get selectedIndex property for listbox

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
3 41045
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: doug.dossett | last post by:
Hello, I have a listbox, where upon clicking a button I want to edit the page associated with the item selected from the list box. I have it check for listboxname.SelectedIndex > -1 for which...
3
by: Zachary Hilbun | last post by:
I'm using a DataGrid control in a web form. I can display it using binding but when I click on any of the items there is no feedback that that row has been selected. I changed SelectedItemStyle...
3
by: Nathan Sokalski | last post by:
I am working on a simple user control composed of 3 DropDownLists that will be used to select Dates. The purpose of the control is to all the user to choose a date using dropdown lists but not need...
1
by: Nathan Sokalski | last post by:
I have a problem that is driving me crazy. I have a User Control composed of three DropDownLists that will be used to select a date. I have everything working except for one thing. When I select a...
6
by: Nathan Sokalski | last post by:
I am writing a User Control that uses 3 DropDownLists. When I attempt to access the SelectedIndex property it incorrectly reports the value selected by the user. Why is this? Here is my code,...
1
by: Alex Stevens | last post by:
Hi All, I've come across something which I think is a bug in the windows forms combobox and listboxes. I have a simple form which retrieves a datatable, and then set the datasource of a...
3
by: Uma sakshi | last post by:
Hi I have one VB.NET application,in that application i have one datagrid control.The datagrid control contains somedata.I want to copy the data in a particular cell and paste it into my C#.NET...
1
by: fiaolle | last post by:
Hi I Can't get the MSDN's example for Listbox to work.The example is below. I get an error in Listbox's sub ListBox1_SelectedValueChanged at row textBox1.Text = ListBox1.SelectedValue and it...
2
by: amtamayo | last post by:
I have a simple webform that has a listbox that I bind to a dataview at designtime. This provides an optional selection for the user so I wanted to have no default value selected. So on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.