0

I have this application that lets the user choose his color preferences and change them later when he decides to do so. The app doesn't need to work between page reloads. I want the user to be able to delete a specific color preference from a drop down list. I want to use indexOf and splice but I have struggled to long with it. Here is what I have got up to now. The trouble is in the Remove(). JS Bin: http://jsbin.com/ANENaRid/2/

Here is the trouble function:

function Remove() {
    var select = document.getElementById("selectColor");
    for (var i = 0; i < colors.length; i--) {
        var selectIndex = colors.indexOf(select.value);
        if (selectIndex !== -1) {
            colors.splice(i, 1);
        }
        return false;
    }
}

6 Answers 6

2

The remove function should look like this and you have to remove the option from the select too (only constructor functions names should be uppercase):

function remove() {
    var select = document.getElementById("selectColor");
    for (var i = 0; i < colors.length; i++) {
        if (colors[i].name_prop == select.value) {
            colors.splice(i, 1);
        }
     }
}
Sign up to request clarification or add additional context in comments.

9 Comments

This is the correct way to find the selected color. However, once found, you can break from the loop. Though, note that it does not do what the author thinks it will. He's only modifying an array. The actual option needs to be removed. In fact, I'd dispense with the colors array and just manipulate options instead (e.g. with jquery).
You should break the loop.
@Zoltan, what do you meen to remove the option from select? This way my other function Place() doesn't work I guess they interfere badly when using the same input 'var select = document.getElementById("selectColor");'
If the user removes a color it must disappear from the dropdown I guess.
It depends on whether or not the data is already bound to the view. Please don't guess, give it an actual check.
|
2
function Remove() {
    var select = document.getElementById("selectColor");
    var selectIndex = colors.indexOf(select.value);
    if (selectIndex !== -1) {
        colors.splice(selectIndex , 1);
    }
    return false;
}

Instead of iterating through the color array you should just select the color you want to remove. You get the index of that item and delete it. No need to iterate it.

Comments

0

you may call the form reset function instead.

remove the style attribute for the example element!

Comments

0

use either

for(var i = 0; i < colors.length; i++) { 
    //your code here
}

or

for(var i = colors.length-1; i >= 0; i--) { 
    //your code here
}

on your loop

and take return statement to inside of if block

1 Comment

var i = colors.length - 1; i >= 0; i--
0

I have refactored you remove function;

function Remove () {
    var selectedIndex = document.getElementById("selectColor").selectedIndex;
  var options=document.getElementById("selectColor").options;
    for(var i = 0; i < options.length; i++) {
            if(options[selectedIndex].text === colors[i].name_prop){
              alert(colors[i].name_prop + "removed");
            colors.splice(i, 1);
               return false;
        }

     }
}

It removes selected item from colors array

2 Comments

It is same as my answer but with added bugs. If you remove from the colors array but not from options, colors[i] can have value undefined, and you will get an exception when asking for name_prop of undefined.
My code is not same as yours. I have developed the code so only it removes selected object from array. Selectbox population is not included in my code
0

This might help a bit :

for (var i = 0; i < colors.length; i++) {
    var index = colors[i].name_prop.indexOf(select.value);
    if (index !== -1) {
        colors.splice(i, 1);
        return false;
    }
}

indexOf is probably useless and not reliable. I'm too lazy to investigate further though, see this answer for an alternate way : https://stackoverflow.com/a/20900571/1636522.

2 Comments

Why do you use indexOf to check equality?
@Zoltan.Tamasi It's a dirty answer based on OP's code. I linked him to yours since I've assumed that you were almost doing it the right way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.