0

Im trying to remove a selected option from a select box in a function. the code below doesn't work. What am I doing wrong?

function removeItem(refundItem) {

    itemToDelete = document.getElementById("yourStuff").options[refundItem].text;
    inCart = document.getElementById("yourCart").rows.length;
    for (i = 1; i <= inCart; i++) {
        maybeRefund = document.getElementById("yourCart").rows[i].cells[0].innerHTML;
        maybeRefund = maybeRefund.trim();
        if (itemToDelete == maybeRefund) {

            refundPrice = document.getElementById("yourCart").rows[i].cells[2].innerHTML;
            document.getElementById("yourCart").deleteRow(i);
            Money.value = Number(Money.value) + Number(refundPrice);
            document.getElementById("yourCart").deleteRow(i);
            document.getElementById("yourStuff").options.remove(refundItem);

            //            document.getElementById("yourStuff").options.remove(selectedIndex);
        }
    }
    document.getElementById("yourStuff").selectedIndex = 0;
}
Tushar
  • 85,780
  • 21
  • 159
  • 179

2 Answers2

0

Your removing from a list while looping through it. I imagine that may be an issue.

Looping through array and removing items, without breaking for loop

Try looping backwards.

Community
  • 1
  • 1
Gerard Downes
  • 643
  • 11
  • 25
0

it should be

..
document.getElementById("yourStuff").remove(refundItem);
..

i.e remove item with index refundItem from select, having id yourStuff. See Select remove() for more info

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162