12

This should be fun to solve :)

In a text field I have the value Apple,Peach,Banana.

Using Jquery I created an array from that CSV.

In HTML I have a list of the fruits with a "remove" option next to each one. When I click "remove" I want to remove the corresponding fruit from the list and the text field.

I'm missing one function that will remove fruits from the array. What function should I use?

http://jsfiddle.net/BXWqK/19/

3 Answers 3

25

You should use JavaScript Splice

fruits_array.splice(fruit_index,1);

You also need to change:

$('#fruits').val(skills_array.join(',')); to $('#fruits').val(fruits_array.join(','));

Sign up to request clarification or add additional context in comments.

1 Comment

6
    var A=['Apple','Peach','Banana'];

    A.splice(1,1)

// removes 1 item starting at index[1] 
// returns ['Peach'];

Comments

2

The accepted solution is correct, but it doesn't mention that you shouldn't use indexOf to get the fruit_index to remove, because IndexOf not Supported in IE8 Browser

You should use:

fruits_array.splice($.inArray('Peach', fruits_array), 1);

Comments

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.