[jQuery] Some jQuery Dom Element Methods

SC Tuan
1 min readAug 7, 2020

--

If you use jQuery select any of element in Dom, it will return a jQuery object but not a real Array .

.toArray

If you want to manipulate the returned jQuery object, you must use .toArray() to convert it.

$('div').toArray().map( // ... do something)

.get()

And you can access it with syntax like an Array .

$('div')[1]

But it is NOT an Array , jQuery provide .get() to fetch the element with index.

$('div').get(1)

And it can receive negative integer.

$('div').get(-1)

Let see an unordered list:

<ul>
<li class="foo1">foo1</li>
<li class="foo2">foo2</li>
<li class="foo3">foo3</li>
</ul>

Here is the index you can use.

<ul>
<li class="foo1">foo1</li> // get(0) get(-3)
<li class="foo2">foo2</li> // get(1) get(-2)
<li class="foo3">foo3</li> // get(2) get(-1)
</ul>

Ref

--

--

SC Tuan
SC Tuan

Written by SC Tuan

iOS developer(Obj-C & Swift) / Web developer (Java, Javascript, CSS,HTML)

No responses yet