Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing options from Select tag in Vanilla JavaScript with "for .. of" loop

Tags:

javascript

When trying to remove options from select, there is always one left, why?

<select id="form-select">   
<option>111</option>
<option>222</option>
<option>333</option>
</select>

This JS is not working:

var t = document.querySelector('#form-select'); 
for(var i of t.options) {
      t.remove(i.index)
    }

And this is not working also:

for(var i of document.querySelector('#form-select').options) {
  i.remove()
}

I know that there are other solutions to achieve that, but i would like to understand why it's not working as it supposed to

like image 936
Piotr Avatar asked Dec 25 '19 23:12

Piotr


People also ask

How do I remove all options from a select Tag?

Just one line to remove all options from the select tag and after you can add any options then make second line to add options. $ ('.ddlsl').empty (); $ ('.ddlsl').append (new Option ('Select all', 'all')); Thanks to the answers I received, I was able to create something like the following, which suits my needs. My question was somewhat ambiguous.

How to remove option from the select list using JavaScript?

The <select> tag specifies that the text that follows is a list, and the <option> tag is used to determine the items of the list. The following examples are used to remove option from the select list with the help of JavaScript. <! DOCTYPE html> <p align="center"> <em> Select and click the button to remove the selected option. </em> </p> <!

How do I add and remove an option in htmlselectelement?

The HTMLSelectElement type represents the <select> element. It has the add () method that dynamically adds an option to the <select> element and the remove () method that removes an option from the <select> element:

How do I add and remove an option dynamically in HTML?

It has the add () method that dynamically adds an option to the <select> element and the remove () method that removes an option from the <select> element: add (option,existingOption) – adds a new <option> element to the <select> before an existing option. remove (index) – removes an option specified by the index from a <select>.


1 Answers

The .options collection is (unfortunately) live, so iterating over the live collection's items one-by-one and .removeing every one will result in every odd one being kept. (Eg, right when you remove the first item, the [0]th item of the collection will immediately become the next item in the collection - what used to be [1] will become [0] (and then once you go to the next index at [1], the new item at position 0 won't be iterated over)

Use document.querySelectorAll instead, which returns a collection which is static:

for (const option of document.querySelectorAll('#form-select > option')) {
  option.remove();
}
<select id="form-select">
  <option>111</option>
  <option>222</option>
  <option>333</option>
</select>

You can also spread into a (static) array before removing elements::

for (const option of [...document.querySelector('#form-select').options]) {
  option.remove();
}
<select id="form-select">
  <option>111</option>
  <option>222</option>
  <option>333</option>
</select>

Another option which just happens to work because the collection is live (but probably shouldn't be used, since it's not intuitive):

const { options } = document.querySelector('#form-select');
while (options.length) {
  options[0].remove();
}
<select id="form-select">
  <option>111</option>
  <option>222</option>
  <option>333</option>
</select>
like image 90
CertainPerformance Avatar answered Oct 09 '22 22:10

CertainPerformance