-1

I have a dropdown. if I select option value in dropdown ,that option should be hidden as result. I attached my piece of code for reference. This working fine in crome but hiding not working in IE-10. suggest in JavaScript it would be helpful.

I have tried Visibility property also. but not working either.

function dropdown(dd) {
  document.getElementById('mySelect').options[1].style.display = "none";
}
<form>
  <select id="mySelect" onchange="dropdown(this)">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
  </select>
</form>

3 Answers3

0

When it comes to the styling of form elements, different browsers have different levels of support. Being that IE is no longer being developed, and that the last updates were years ago, you're going to find behaviors that just don't work there and never will.

Now, your code does work in browsers that support option styling of this kind, it just always sets the second option element to be hidden because you are hard-coding 1 as the index. Instead, set the .selectedIndex of the select to be not visible so that you always get the index of whatever option is currently selected. And, that will hide the last selected option from showing when the list is subsequently shown, however the select will still show that value so you also have to remove that.

document.getElementById("mySelect").addEventListener("change", dropdown);

function dropdown() {
  this.options[this.selectedIndex].style.visibility = "hidden"; // Hide on the list
  this.value = ""; // Hide the new value
}
<form>
  <select id="mySelect">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
  </select>
</form>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

Its kind of another way

<form>
  <select id="mySelect" onchange="dropdown(this)">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
  </select>
</form>
<script>
function dropdown(dd) {
  var a = document.getElementById('mySelect');
  a.remove(a.selectedIndex);
}
</script>
Punitha Subramani
  • 1,467
  • 1
  • 8
  • 6
0

If you want to hide on change only, another on change if you again the data.

You can try it.

<form>
  <select id="mySelect" onchange="dropdown(this)">
    <option>--Select</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
  </select>
</form>
<script>
var mySelect = [];
function dropdown(dd) {
  var mySelect1 = document.getElementById('mySelect');
  if(mySelect.text !== undefined && mySelect.index !== '') {
    mySelect1.options.add(new Option(mySelect.text, mySelect.index), mySelect1.options[mySelect.index]);
  }

  var selectedText = mySelect1.options[mySelect1.selectedIndex].text;

  mySelect.text = selectedText;
  mySelect.index = mySelect1.selectedIndex;
  mySelect1.remove(mySelect1.selectedIndex);
}
</script>
Punitha Subramani
  • 1,467
  • 1
  • 8
  • 6