so I have an Array that is filled with a bunch of repeats like ['hey','hey','yeh'] and I have a html form that removes any entry from my Array if the submitted text matches it. However, my JS/jQuery doesn’t seem to be functioning the way I like. Only the first few entries seem to delete every time I press the button, as opposed to all the matches (which is what I want). Shouldn’t my “each” function be going through all the items in my array and deleting all of the entries that match whatever was submitted?
HTML:
<form id="remove_user" action="#" method="post">
<label for="user_num">Remove user:</label>
<input type="text" id="user_num" name="user_num" placeholder="number">
<input type="submit" value="(-) remove">
</form><!-- #remove_user -->
<ul id="user_list"></ul><!-- #user_list -->
JS updated with a change, but still doesn’t work:
$('#remove_user').submit(function(){
id = $('#user_num').val();
$.each(myArr, function(i, value){
if (value == id){
myArr.splice(i, 1);
};
});
$('#user_list').html('');
for (var i=0; i < myArr.length; i += 1) {
$('#user_list').append('<li>' +myArr[i]+ '</li>');
};
return false;
});
hey i think this should be like this.
};
EDIT
FIDDLE