Actually the [0] doesn't convert anything, and it doesn't remove or replace
any methods. You could think of it that way, but here's a more accurate way
to look at it:

The jQuery object is an array of DOM elements. (It's not an actual Array
object, but it is an array-like object with [0], [1], etc. and .length
properties.) When you use [0], you are simply getting a reference to the
first DOM element in this array, without affecting the jQuery object itself.

Consider this code:

    var $div = $('div.selectable');
    var div0 = $div[0];

Now $div is the jQuery object with all of its usual methods, and div0 is the
first DOM element in the jQuery object's array of elements. Doing the
$div[0] didn't have any effect on $div - it still has all of the normal
jQuery methods.

It's very much like this code:

    var array = [ 'a', 'b', 'c' ];
    var first = array[0];
    alert( array.length );  // 3
    alert( first.length );  // 1

In this case it's more obvious that doing the array[0] doesn't have any
effect on the original array. It's merely fetching a reference to the first
array element, which happens to be a string.

I hope this doesn't seem too nitpicky! :-) Just trying to help illustrate
how it actually works.

-Mike

> From: Ryura
> 
> The glue code is converting your jQuery object.
> 
> var text = $('div.selectable')[0].getSelection();
> alert(text);
> 
> [0] takes the first element you selected and "removes" the 
> jQuery methods, "replacing" them with the usual DOM methods.

Reply via email to