Remove item from jQuery object

I have a jQuery object that is created through jQuery .find()as shown below ...

var $mytable= $('#mytable');
var $myObject = $mytable.find("tbody tr");

This works fine and creates a jQuery object of all the elements trin tbody. However, as I iterate over the data, I need to delete parts of the object when I go. For example, if the above call returns a jQuery object with a name of $myObjectlength 10, and I want to remove index 10, I thought I could just do $myObject.splice(10,1)it and it will delete the element with index 10. However, this does not seem to work.

Any ideas why? Thanks!

UPDATE

Basically I just want to remove any element that I want from $ myObject when I go through the data. I know this is based on level zero (a bad example higher than I assume), just trying to understand my point of view.

UPDATE

Ok, so I create an object using the find method in the table, and when I create it, the length is 24. When I iterate over the object, when I hit the element, I don’t want me to try to use Array.prototype.splice.call ($ rows, x, 1), where x represents the index to delete. Subsequently, when I view the object in the console, it still has a length of 24.

+4
source share
6 answers

splice is not part of the jQuery API, but you can use your own Array methods in jQuery collections using a prototype:

Array.prototype.splice.call($myObject, 9, 1); // 0-index

pop :

Array.prototype.pop.call($myObject);

length.

+2

.not(), , jQuery :

var $myObject = $mytable.find('tbody tr').not(':eq(9)'); // zero-based

http://jsfiddle.net/mblase75/tLP87/

http://api.jquery.com/not/


:

var $myObject = $mytable.find("tbody tr:lt(9)");

http://jsfiddle.net/mblase75/9evT8/

http://api.jquery.com/lt-selector/

+3

splice - , - jQuery.

slice

+1

Javascript . , (.. 10- ) 9.

$myObject[9]

, - :

$myObject.splice(9, 1);

.

0

.remove DOM.

So, to remove an element at index 9 of an array $myObject, use:

$myObject.eq(9).remove();

If you want to save the item that you are deleting, you can also do:

var removedElement = $myObject.eq(9);
removedElement.detach();
0
source

You can also use a filter:

var $myObject = $mytable.find("tbody tr").filter(':lt(9)');
0
source

All Articles