Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    options.selectedValue keeps coming back as "undefined"

    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.

    Code:
    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.

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have you tried... this.options[selectedIndex].value?
    *this message will self destruct in n-seconds*

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ... okay, for starter, i had selectedValue.

    ...

    you were close, senor boom.

    Code:
    		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...

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops, type-o...
    *this message will self destruct in n-seconds*

  • #5
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).

  • #6
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sounds like a plan...
    *this message will self destruct in n-seconds*

  • #7
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •