Reputation: 1855
I'm failing to remove a string from a list using javascript together with knockout. Any advice appreciated, many thanks, James
See this example: http://jsfiddle.net/rxkU3/4/
Code snippet
viewModel.toRemove = ko.observable();
viewModel.remove = function() {
viewModel.Article.Keywords().remove(viewModel.toRemove());
}
In Chrome's console I get the error: "has no method 'remove'"
Upvotes: 1
Views: 64
Reputation: 8520
The expression viewModel.Article.Keywords()
is a plain string, not an array. Therefore, there is no remove
function.
If the data does indeed return the list deilmited by pipes, then you can try to call the replace
function on the string to remove the value (this doesn't account for removing the first value, and will also remove multiple entries if they have the same value):
var newKeywords = viewModel.Article.Keywords().replace("|" + viewModel.toRemove(), "");
viewModel.Article.Keywords(newKeywords);
Fiddle: http://jsfiddle.net/rxkU3/6/
However, I don't recommend this solution. It is much better and less error-prone to use an actual array if possible.
In that case, you can simply call Keywords.remove()
to remove an entry from the array.
See this fiddle (split original string into an array): http://jsfiddle.net/rxkU3/8/
or
See this fiddle (data in JSON is already an array): http://jsfiddle.net/rxkU3/7/
Upvotes: 5