search

Home  >  Q&A  >  body text

The behavior of select.remove() method is puzzling

<p>So I work with JavaScript in AgilePoint. I've implemented a function that removes a specific option from a dropdown menu, but the end result is very strange. Here is sample code: enter image description here</p> <p>So here I have set a simple condition, if the condition is true, I want to remove the first option, the third and the fourth option. But in the end, it only removed the first and fourth options, leaving the third option still there. enter image description here</p> <p>The last option also needs to be removed, but I don't understand why it ignores the second line. </p>
P粉322918729P粉322918729474 days ago524

reply all(1)I'll reply

  • P粉818125805

    P粉8181258052023-08-19 00:11:11

    The reason is that if you run the following code to remove the 0th element:

    select.remove(0);

    The 2nd and 3rd elements will no longer be the 2nd and 3rd, but become the 1st and 2nd because the 0th element has been removed.

    The quick solution is to remove from the largest index to the smallest index:

    select.remove(3);
    select.remove(2);
    select.remove(0);

    reply
    0
  • Cancelreply