Disable the previous and next options in a select box?

Discussion in 'JavaScript' started by PedstersPlanet, Aug 6, 2009.

  1. #1
    Is this possible? Say someone selected a select option, is there a way to disable the previous (if available) and next (if available) option to the their selected one?
    Ofc it'll need to use a 'onChange' trigger, but I'm not sure how to do the function part.

    Any ideas?
     
    PedstersPlanet, Aug 6, 2009 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    So you want a JS function that disables other option tags when selected? Well there are a couple ways you can do that, but I'll out line the idea that came to my head;

    
    <!-- HTML SIDE -->
    
    <select id="selectBox1" onchange="onSelectChange(this.id,this.value);" >
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    </select>
    
    HTML:
    
    // JS SIDE //
    
    function onSelectChange(targetElement, newValue){
    	if(newValue = "3"){
    		runDisableOption(targetElement, 0);
    		runDisableOption(targetElement, 3);
    	}
    
    	if(newValue = "1"){
    		runEnableOption(targetElement, 0);
    		runEnableOption(targetElement, 3);
    	}
    }
    
    function runDisableOption(targetSelect, targetNode){
    	var optionNodeArray = document.getElementById(targetSelect).childNodes;
    	optionNodeArray[targetNode].disabled = true;
    }
    
    function runEnableOption(targetSelect, targetNode){
    	var optionNodeArray = document.getElementById(targetSelect).childNodes;
    	optionNodeArray[targetNode].disabled = false;
    }
    
    Code (markup):
    Or something along those lines, I haven't tested the code- so it's just theory. . .
     
    Last edited: Aug 6, 2009
    ToddMicheau, Aug 6, 2009 IP
  3. PedstersPlanet

    PedstersPlanet Peon

    Messages:
    195
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks.... I forgot to say its a multi-select box, will this theory still work?

    I've attached a screenshot to show that some options are disabled already (by default (by PHP)) so I do not want those disabled options to be enabled again - if you get me.
     

    Attached Files:

    PedstersPlanet, Aug 7, 2009 IP