Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select box validation (SelectedIndex == -1)

Status
Not open for further replies.

hazelsisson

Programmer
Mar 18, 2002
68
GB
Hi guys,
here's an annoying problem that you may be able to help me with:

I have two multiple select boxes side by side, which can have options swapped by clicking on an "add" or "remove" button.
The problem is if nothing is selected and "add" or "remove" is pressed an error shows, which is "Object doesn't support this property or method". Here's the code:
Code:
function addValue() 
{
var theIndex;
var theIndex = document.normForm.normUsers.selectedIndex;

 if (theIndex == -1 )
 {
 alert("You have not selected anything to move!");
 }
 else
 {
 //put option in other select box:
 ...		
 }
}

<SELECT SIZE=&quot;6&quot; NAME=&quot;normUsers&quot; MULTIPLE=&quot;Yes&quot; onDblClick=&quot;addValue();&quot;>
(Options are dynamically generated) 
</SELECT>

<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;addButton&quot; VALUE=&quot;->&quot; onClick=&quot;addValue();&quot;>
Let me know if you want to see any of the other code. (I haven't got the name of the form wrong!)

Thank-you!
Hazel
 
try an alert()


var theIndex;
var theIndex = document.normForm.normUsers.selectedIndex;
alert(theIndex)
if (theIndex == -1 )

Known is handfull, Unknown is worldfull
 
Thanks vbkris.

I tried the alert:
If something was selected it showed the correct index as it should, but if nothing was selected it didn't even get to the alert (the error appeared!).

Do you think it could be something to do with the variables being listed at the beginning of the function? There are other variables that use &quot;theIndex&quot;, such as:
Code:
theName = document.normForm.normUsers.options[theIndex].text;

Thanks for your help.
 
it may be anything, try puttin a dummy alert between stateents that u think might cause the problem, anyway ur error will also give the line number where the erro was found...

Known is handfull, Unknown is worldfull
 
It's sorted!

If anyone ever has the same error it's due to the variables being assigned their values before it was legal to do so.

Thanks vbkris!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top