Phil
Phil

Reputation: 4069

Remove element from jQuery object

I have a jQuery object that is created via jQuery .find() as seen below...

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

This works great and creates a jQuery object of all the tr elements in the tbody. However, as I'm looping over the data, I need to be able to remove parts of the object as I go. For instance, if the above call returns a jQuery object named $myObject with a length of 10, and I want to remove the index 10, I thought I could just do $myObject.splice(10,1) and it would remove the element at index 10. However this doesn't seem to be working.

Any ideas why? Thank you!

UPDATE

I basically just want to be able to remove any element I want from $myObject as I loop through the data. I know it's zero based (bad example above I guess), was just trying to get my point across.

UPDATE

Okay, so I create the object using the find method on the table and at it's creation it's length is 24. As I loop over the object, when I hit an element I don't want I tried to use Array.prototype.splice.call($rows,x,1) where x represents the index to remove. Afterwards when I view the object in the console, it still has a length of 24.

Upvotes: 4

Views: 4939

Answers (6)

Tom Heard
Tom Heard

Reputation: 1278

You can use .remove to remove an element from the DOM.

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

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

If you want to keep the element that you are removing, you can also do:

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

Upvotes: 0

David Hellsing
David Hellsing

Reputation: 108480

splice is not part of the jQuery API, but you can apply native Array methods on jQuery collections by applying the prototype:

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

You can also use pop to remove the last item:

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

This should also give you a correct length property.

Upvotes: 2

Blazemonger
Blazemonger

Reputation: 92893

Use .not() to remove a single element, then loop through the jQuery object at your leisure:

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

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

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


Or if you might be removing more than one:

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

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

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

Upvotes: 5

Karl-André Gagnon
Karl-André Gagnon

Reputation: 33870

You could also use filter :

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

Upvotes: 0

Peter Di Cecco
Peter Di Cecco

Reputation: 1578

splice is an array method, not a jQuery object method.

Try slice

Upvotes: 1

clansaur
clansaur

Reputation: 751

Javascript uses zero-based arrays. This means that the final item in the array (i.e. the 10th item) will be at index 9.

$myObject[9]

So you need something like this:

$myObject.splice(9, 1);

This will remove the element from your existing array, and also return it.

Upvotes: 0

Related Questions