473,513 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

selectedIndex Bug?

It's been quite awhile since I've written anything in JavaScript, so
please pardon my ignorance if I'm missing the obvious. The following
code works fine in Firefox, but not in IE (unless I include an alert).

The piece of code that's giving me grief is:
function doThis() {
// alert('Activate this line makes it work');
var iSelectedValue =
document.getElementById('mySelect').options[document.getElementById('mySelect').selectedIndex].value;
alert(iSelectedValue);
}

I get an "Object doesn't support this property or method" error in IE
on the first time I run the function, subsequent calls to the function
don't give me an error, but do give me screwy results. If I uncomment
the alert before the line with the selectedIndex reference everything
works fine in IE.

The full example is at www.velocitypath.com/example1.html

Any help is greatly appreciated!

Sep 13 '06 #1
4 3107

af*****@gmail.com wrote:
The piece of code that's giving me grief is:
function doThis() {
// alert('Activate this line makes it work');
var iSelectedValue =
document.getElementById('mySelect').options[document.getElementById('mySelect').selectedIndex].value;
alert(iSelectedValue);
}

I get an "Object doesn't support this property or method" error in IE
[..]
The full example is at www.velocitypath.com/example1.html
Actually, the code that's giving you grief is your:

<select id="mySelect" multiple="multiple" size="10"
onClick="doThis();">

Change "onclick" to "onchange" and all will work fine. Apparently the
onclick is firing first, and you're simply getting the previous
selectedIndex, which on the first click would be -1.

Cheers - Kev

Sep 13 '06 #2
af*****@gmail.com wrote:
It's been quite awhile since I've written anything in JavaScript, so
please pardon my ignorance if I'm missing the obvious. The following
code works fine in Firefox, but not in IE (unless I include an alert).

The piece of code that's giving me grief is:
function doThis() {
// alert('Activate this line makes it work');
var iSelectedValue =
document.getElementById('mySelect').options[document.getElementById('mySelect').selectedIndex].value;
Ignoring the rather horrible code, the issue appears to be related to
the select element's multiple attribute. In this case, it seems that
IE does not allocate a default selected option. doThis() is called
onclick, so when called for the very first click, the selectedIndex at
that point returns -1. There is no option with index -1 so an error
results. I suspect that the alert allows time for the click to filter
through and cause an option to be selected, hence the error doesn't
occur in that case.

There are a couple of solutions, but that depends on what you are
actually trying to do.

Incidentally, if you ctrl+click to select multiple options, you only
get the selected index of the first selected option.

I would pass 'this' from the select, so the code is more like:

function doThis(sel){
var selectedValues = [];
for (var i=0, len=sel.length; i<len; i++){
if (sel[i].selected) {
selectedValues.push(sel[i].value);
}
}
alert('You selected:\n' + selectedValues.join('\n'));
}

But you'll note that the result is still one click behind.
alert(iSelectedValue);
}

I get an "Object doesn't support this property or method" error in IE
on the first time I run the function, subsequent calls to the function
don't give me an error, but do give me screwy results.
The results are quite predictable - IE reports the previously selected
option, which indicates that onclick runs before the selectedIndex
property is changed. Doing:

<select id="mySelect" multiple="multiple" size="10"
onClick="alert(this.selectedIndex);">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>

Shows -1 the first time the select is clicked on.
--
Rob

Sep 13 '06 #3
Kev and Rob,

Thank you so much! I've switched to the onChange event. That fixed it.
I've also updated to pass "this". Feel good that you've helped someone
today!

Sep 13 '06 #4
ASM
af*****@gmail.com a écrit :
It's been quite awhile since I've written anything in JavaScript, so
please pardon my ignorance if I'm missing the obvious. The following
code works fine in Firefox, but not in IE (unless I include an alert).
Try to slowly explain to IE what exactly you want ?

function doThis() {
var selct = document.getElementById('mySelect');
var idx = selct.options.selectedIndex;
var iSelectedValue = selct.options[idx].value;
alert(iSelectedValue);
}

Probably better
if you could have a form named ( <form name="myForm" ...)
and the select too ( <select name="mySelect" id="mySelect" ... )
and use the old way to do :

function doThat() {
var opts = document.forms['myForm'].mySelect.options;
var iSelectedValue = opts[opts.selectedIndex].value;
alert(iSelectedValue);
}

--
Stephane Moriaux et son [moins] vieux Mac
Sep 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4517
by: ross kerr | last post by:
Hi all, I have a control that extends the ComboBox object. It updates the selected item based on what the user enters in the text area. In the OnLeave event of the combobox, the selected...
3
4663
by: Nikhil Patel | last post by:
Hi all, I am using the standard System.Windows.Forms.ComboBox controls on a form. There are 5 or 6 comboboxes and their selection depends on each other. The comboboxes behave strangely probably...
3
1681
by: Angel | last post by:
I have a problem I placed a DropDown list control on my .NET page. I do not want to perform a postback when something is selected. When I want to get what was selected in the Client Script I do not...
3
2386
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
1643
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
1703
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
3149
by: Jules | last post by:
We have a problem setting, actually resetting, the SelectedItemTemplate of a DataList control. Below is the ObjectDataSource and the DataList in .ASPX: <asp:ObjectDataSource ID="mySource"...
1
3217
by: marcbb | last post by:
Hi all, I have a really strange problem working with Dropdownlists in a DataGrid. I'm trying to preselect some values from the DropDownlist for each row in the DataGrid, but it seems that...
18
2100
by: Academia | last post by:
I let the use modify the text of a combobox and then I replace the selected item with the new text (in Keyup event). But if he sets the Text property to an empty string ("") that sets the...
3
1896
by: S_K | last post by:
Hi, I have a list of 6 DropDownList boxes, from DropDownList1 to DropDownList6, and I'm changing the SelectedIndex of each using a foreach loop as follows: foreach(PaymentReqDisplay...
0
7260
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
7384
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,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.