Javascript - Form Select Box syntax for selected option?

You are looking for: if (document.myform.myselectbox. SelectedIndex! = -1) When there is nothing selected the index returns -1.

If you actually want the internal value or text string for the selected option you can access it by index: var selObj = document.myform. Myselectbox; var selIndex = selObj. SelectedIndex; var selOptionValue = selObj.optionsselIndex.

Value; var selOptionText = selObj.optionsselIndex. Text; However you need to be aware that the behavior is also a bit dependent on how you have it displayed. With a "single" select element (e.g. A "drop down") if you don't specify that a particular option is "selected" then the first option (index 0) is considered to be selected as that is how it is visually displayed.

Red orange yellow ... If you have a select element with a size attribute greater than 1 (e.g. 6) then visually there are none selected, thus the element will return -1 by default (if none were selected) red orange yellow ... Either way, you can use code like this to determine what to do: var mySelect = document.myform. Myselectbox; var selIndex = mySelect. SelectedIndex; if(selIndex!

= -1){ //an option is selected if(selIndex == 0){ //first option is selected } else if(selIndex == 1){ //second is selected } else if(selIndex == 2){ //third is selected } } else { //no option is selected } You could write this using a switch/case statement too, I've just expanded this to indicate a few values.

I have a simple drop down. Three options. Based on a certain option selection I need to one, make visible (vs. hide/show) a form element, and two, change the class of another form element to "required".

– adg Apr 18 at 13:35 @adg - in that case getting the . SelectedIndex will correctly identify for you which item is selected for your concerns. The "warning" is only for those that want to determine if the user physically selected an option in which the .

SelectedIndex may return a "false positive" if not combined with a change event listener. – scunliffe Apr 18 at 13:49 All above my head :) I'm super new to this, so alot of the code-speak only confuses me further. :) :) Anyway, if I have "if (document.myform.myselectbox.

SelectedIndex! = -2), then my " document. GetElementById("two").style.

Visibility = "visible";" should fire off if "two" was the actual 2nd option in the list? – adg Apr 18 at 14:13 I added a jsfiddle link, above. I can't seem to get this working.

A JS error on page as well when run via normal means. – adg Apr 18 at 14:55 The -1 value is special. It is used in JavaScript (and Java and others) to be an integer value with the special meaning of "not found" or "no index" etc.Its used in cases where all the possible indexes would be positive.

E.g. In a list of 3 items, 0 is the first, 1 is second, 2 is the third (because Arrays in JavaScript are zero-indexed). I'm adding code to the answer to show some ideas of how to handle the value returned.

– scunliffe Apr 18 at 16:49.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions