FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

getElementsByStyle

20mins

Implement a function getElementsByStyle(element, property, value) that searches through all descendants of a given DOM element and returns an array of elements whose computed style matches the specified property/value.

Requirements:

  • Only search descendants of the provided element (exclude the element itself).
  • Return an array of matching Elements, not an HTMLCollection.
  • Do not use document.querySelectorAll.
  • You may find window.getComputedStyle useful.

Example

const doc = new DOMParser().parseFromString( `<div> <span style="font-size: 12px">Span</span> <p style="font-size: 12px">Paragraph</p> <blockquote style="font-size: 14px">Blockquote</blockquote> </div>`, 'text/html' ); getElementsByStyle(doc.body, 'font-size', '12px'); // returns [span, p] (array of elements)

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests