I'm working on a legacy project (before Rails 3.1) which has been around for years, so it has Prototype and jQuery all over the place with the general idea to rewrite all the Prototype in jQuery.
I'm working on a problem of an autocomplete style dropdown, which will populate an area in the DOM with some data. This works fine, but certain instances of this autocomplete have an item limit. So when adding the data, it will check to see if there are more than N of a certain selector and remove the last one.
My function to do this is straight forward:
function remove_items(num, selector) {
while (selector.length >= num) {
selector[selector.length-1].remove()
}
};
I pass in the limit and the array of the objects, selected with jQuery, I want there to be a limit on.
The problem appears to be that .remove() on an element in selector will remove it from the DOM using the Prototype method, even though it's a jQuery object. Then, because the selector array hasn't been changed, the loop runs again with the prototype method and it throws an error because now the element isn't in the DOM.
I made a count variable of the length of selector before the loop and reduce this by one each time, but obviously this is a less than ideal solution.
Anyone know why this is happening or how to stop it happening?