:::: MENU ::::

jQuery toArray() function vs pure javascript code example


jQuery toArray() method

toArray() method applies to matched jQuery DOM elements. It returns array of these matched element objects.

Example :- Get array of all table row objects.

It returns array of all <tr> objects, but not jQuery(“<tr>”) objects

So, If you want to use any jQuery method each() over them, it won’t work.

 Then, why to use jQuery toArray() method?

There are many javascript native functions not available to jQuery objects as methods, you could use toArray() to get plain object then apply native DOM methods over them.

Demo usage:- Highlight all list items start with string PHP

jQuery toArray() example


Here, String.match(/regex/) method doesn’t available in jQuery so you could use it over native HTMLElement objects.

Alternative pure javascript function for jQuery toArray() method

Try the same demo with pure javascript code

So, what do you think ?