I have an object with string values attached to the same property that I wish to remove using a function if that value is present. To achieve this I have placed the string values into an array, iterated through them and want to remove the value using splice()
. The problem is that splice()
doesn't seem to recognize the array counter. It always removes the first item in the array even when the counter is is higher. I've checked the code and the loop is working correctly.
I've not seen anything online to suggest that splice
can't be used with a variable at the index. How can I make splice
use the counter to remove the relative value ?
var obj = {
className: 'open menu next thing'
}
function removeClass(elem, cls) {
var arr = elem.className.split(' ');
for(var i = 0; i < arr.length; i++) {
if(arr[i] == cls) {
alert(arr[i]);
var splCout = arr.splice(arr[i], 1);
alert(splCout);
}
}
var str = arr.join(' ');
alert(str);
}
removeClass(obj, 'next');
splice
is not a value, it's a start index. You should write somethnig likearr.splice(i, 1);
– u_mulder Jan 4 '14 at 14:07