开发者

Remove item from dropdown list on page load (no jquery)

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 Javasc开发者_StackOverflow中文版ipt on page load so the user never sees that option?

Thanks


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

for (i=0;i<select.length;  i++) {
   if (select.options[i].value=='D') {
     select.remove(i);
   }
}


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/


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');


var selectbox = document.getElementById('DD1');
for(var i = 0; i < selectbox.options.length; i++)
{
    if(selectbox.options[i].value == 'D')
    selectbox.remove(i);
}


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);


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.


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

var Node1 = document.getElementById("DD1");
Node1.removeChild(Node1.childNodes[1]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜