5

I have the following dropdown list:

<select name="DD1" id="DD1">
    <option value="B">B</option>
    <option value="D">D</option>
    <option value="E">E</option>
    <option value="F">F</option>
    <option value="R">R</option>
</select>

On page load I need to hide/delete option D. I can't go by the index because that list is dynamic so I have to use the value parameter or the actual text that gets rendered.

I've tried finding the answer but all the solutions I came across use JQuery for this and I don't have the option to do this.

Anyone have a way to hide option D just using Javascipt on page load so the user never sees that option?

Thanks

markiyanm
  • 348
  • 1
  • 5
  • 18

7 Answers7

13
var select=document.getElementById('DD1');

for (i=0;i<select.length;  i++) {
   if (select.options[i].value=='D') {
     select.remove(i);
   }
}
Alex
  • 34,899
  • 5
  • 77
  • 90
VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
5

I used window.addEventListener it won't work on down-level browsers that don't support it. I suggest creating your own addEvent method that abstracts the inconsistencies - if jQuery (or some other framework) is not allowed.

window.addEventListener("load", function(){
   var list = document.getElementById('DD1'), el;

   for(var i in list.children){
       el = list.children[i];
       if(el.hasOwnProperty('value')) {
          if(el.value == 'D') {
             el.style.display = 'none';
             break;
          }
       }
   }
}, false);

http://jsfiddle.net/UV6nm/2/

Alex
  • 34,899
  • 5
  • 77
  • 90
1

Try looping over the options, checking the value, and if it matches, set it to null:

function removeByValue(id, value) {
    var select = document.getElementById(id);
    for (var i=0, length = select.options.length; i< length; i++) {

        if (select.options[i] && select.options[i].value === value) {
            select.options[i] = null;
        }
    }
}
removeByValue('DD1', 'D');
circusbred
  • 1,240
  • 8
  • 8
1
var selectbox = document.getElementById('DD1');
for(var i = 0; i < selectbox.options.length; i++)
{
    if(selectbox.options[i].value == 'D')
    selectbox.remove(i);
}
Nick Tiberi
  • 1,132
  • 12
  • 20
0

Since everyone else has proposed basically the same solution, here's a simpler solution if you have the benefit of only targeting modern browsers:

var el = document.querySelector('select[name=DD1] option[value=D]');
el.parentNode.removeChild(el);

Or, for every browser:

var el;
if(typeof document.querySelector == 'function') {
  el = document.querySelector('select[name=DD1] option[value=D]');
} else {
  for(var child in document.getElementById('DD1').childNodes) {
    if(child.textContent == 'D') {
      el = child;
      break;
    }
  }
}

el && el.parentNode.removeChild(el);
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
0

There's probably a little cleaner way to do this but I am rusty with javascript.

var options = documentgetElementsByTagName("option");
for(i=0; i<options.length; i++)
{
   if(options[i].value == "D")
   {
     this.class += "displayNone";
   }
}

.displayNone{ display: none; }

this is not tested so I don't know if it would work.

CBRRacer
  • 4,649
  • 1
  • 23
  • 27
  • `document.getElementsByTagName("option")` returns a collection `.value` is not a property. It will **break** – Alex Sep 29 '11 at 18:51
-1

you don't need this. just a two line code is enough.

var Node1 = document.getElementById("DD1");
Node1.removeChild(Node1.childNodes[1]);
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
xecute
  • 446
  • 8
  • 23