Jamie Hutber
Jamie Hutber

Reputation: 28076

Convert jquery selectors to array

I need to be able to store the current selectors in the current viewport and then 10 seconds check if they are still in the users current viewport.

My solution for this was to store the selectors in an array and then in 10 seconds compare the old selectors against the new and see if any match. If they do... do something.

So i believe using .each and build the array, unless somebody has a more elegant solution to this?

$('.gridContainers:in-viewport')

This will return a standard selectors.

Upvotes: 5

Views: 12990

Answers (3)

Abdennour TOUMI
Abdennour TOUMI

Reputation: 93203

With ES6 :

Array.from($(selector)); // return JS array 

Upvotes: 1

Hacknightly
Hacknightly

Reputation: 5144

Calling $(selector) returns an array-like jQuery object, not an actual JavaScript array, though for the purposes of what they're trying to do converting it to an actual array may be unnecessary.

This is how one would turn a selector into an native Javascript array.

$(selector).toArray()

Jquery.toArray()

Upvotes: 17

Mohan
Mohan

Reputation: 1081

Try Out FIND method as Below:

$('element').find('selection');

This will give all selected elements to Array. Hope this helps

Upvotes: 0

Related Questions