jquery get only visible element in javascript

In jQuery, you can use the :visible selector to get only the visible elements. To achieve this using plain JavaScript, you can iterate through all the elements and check if they are visible using the offsetHeight and offsetWidth properties.

index.tsx
// using jQuery
var visibleElements = $(':visible');

// using plain JavaScript
var allElements = document.querySelectorAll('*');
var visibleElements = [];
for (var i = 0; i < allElements.length; i++) {
  if (allElements[i].offsetWidth > 0 && allElements[i].offsetHeight > 0) {
    visibleElements.push(allElements[i]);
  }
}
326 chars
12 lines

This code will return an array of only the visible elements on the page.

gistlibby LogSnag