...

View Full Version : options.selectedValue keeps coming back as "undefined"



]|V|[agnus
03-04-2005, 09:41 PM
I've got a form with a series of <select /> menus. Each menu has a few defined options, and then an "Other" option. When the "Other" option is selected, I want to enable a <input type="text" /> for the user to fill in details for "Other".

My function, below, is run at page load. First it checks for the existence of the <fieldset /> that contains the aforementioned <select /> menus. Then it loops over the <selects /> and they're associated <input />s and assigns functions to various events, etc.



function disenableOthers() {
var details = document.getElementById("details");
if (details) { // if this exists, we are at the "add job" page where this is used

var others = details.getElementsByTagName("input");
var otherNotice = "Enter other option here..."; // used later, you'll see
for (var o = 0; o < others.length; o++) {
others[o].disabled = true; // disable by default
others[o].onfocus = function() {if (this.value == otherNotice) {this.value = "";}};
others[o].onblur = function() {if (this.value == "") {this.value = otherNotice;}};
}

var selects = details.getElementsByTagName("select");
for (var s = 0; s < selects.length; s++) {
selects[s].onchange = function() {
var theOther = document.getElementById(this.getAttribute("id") + "Other");
alert(this.options.selectedValue); // for debugging
if (this.options.selectedValue == "Other") {
theOther.disabled = false;
theOther.value = otherNotice;
} else {
theOther.disabled = true;
theOther.value = "";
}
};
}

}
}


Initially, I just kept getting the "Other" boxes enabled no matter WHAT I selected, but with what I've got up there now, they are never enabled. :(

codegoboom
03-05-2005, 12:18 AM
Have you tried... this.options[selectedIndex].value?

]|V|[agnus
03-05-2005, 01:57 AM
... okay, for starter, i had selectedValue.

... :o

you were close, senor boom. :)



var selects = details.getElementsByTagName("select");
for (var s = 0; s < selects.length; s++) {
selects[s].onchange = function() {
var theOther = document.getElementById(this.getAttribute("id") + "Other");
if (this.options[this.selectedIndex].value == "Other") {
theOther.disabled = false;
theOther.value = otherNotice;
} else {
theOther.disabled = true;
theOther.value = "";
}
};
}


selectedIndex by itself means nothing, and it is not a property of the options array, but the reference to the select element itself.

i feel like a weight has been lifted off my soul...

codegoboom
03-05-2005, 02:00 AM
Oops, type-o... :D

]|V|[agnus
03-05-2005, 02:14 AM
i'm refining the script right now. i've got a much better script that i will try to remember to post in the "post a script" forum(with much improved comments).

codegoboom
03-05-2005, 02:33 AM
sounds like a plan... :cool:

]|V|[agnus
03-05-2005, 02:49 AM
http://www.codingforums.com/showthread.php?t=53757

much better. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum