user1899082
user1899082

Reputation:

Difference between slice method and get method

I was reading a book and for slice method it gives an example like this:

$('*').slice(2,3);

so to me it looks like it is saying Ok first select all elements on the page, then return the third element.

But then the book says note that this is different from $('*').get(2) Which makes me confused, feeling I have learned nothing so far. It is saying the latter

returns the third "element" in the wrapped set but the first one returns a wrapped set containing the element.

I don't understand the difference, wrapped set is different from element?

Upvotes: 1

Views: 593

Answers (3)

mlwacosmos
mlwacosmos

Reputation: 4561

Well, as it is said with 'slice' function you can retrieve a set (more than one object) and with the 'get' function you just have one element.

But the important thing is that 'get' function returns an element of the DOM (it is not a jquery object, whereas slice function returns a jquery object.

For example if you want to change the background color :

$("*").get(8).style.backgroundColor = "red";
$("*").slice(8,9).css("background-color", "red");

Upvotes: 1

Jon
Jon

Reputation: 437584

Yes, there is a difference.

This is a DOM element:

var element = document.getElementById("foo");

This element exposes those properties and functions that are specified by the DOM (MDN reference). For example, assuming it's an input element you can get its value with element.value.

This is a jQuery object, also known as a "wrapped set":

var wrapped = $(element);

This particular wrapped set contains just the one element from above. The wrapped set exposes properties and functions as defined by jQuery. For example, you can get its value with wrapped.val().

Let's see what the book notes discuss.

First quote:

the latter returns the third "element" in the wrapped set

This means that the return value of .get() is one of the wrapped elements; when you use it, jQuery hands you a reference to one of the elements it was wrapping. The API documentation explicitly states that the result is a DOM element:

Retrieve one of the DOM elements matched by the jQuery object.

Second quote:

but the first one returns a wrapped set containing the element

This means that the return value of .slice is itself a wrapped set that wraps only some of the elements the source wrapped set was wrapping. The API documentation describes says that the result is a wrapped set that wraps a subset of the original:

Reduce the set of matched elements to a subset specified by a range of indices.

Upvotes: 4

Wallysson Nunes
Wallysson Nunes

Reputation: 750

The first option using slice will return not just the third element, but all elements after the second, as you passed 3, it will return only the third element.

But if you pass $('*').slice(2,5); it will return the the third, fourth and fifth element, the $('*').get(2) returns only the second element, it doesnt need any parameter to set who would be the first element to start as slice as it returns just one element.

Upvotes: 0

Related Questions