Frontend101 logoFrontend101

Implementing getElementsByStyle() in JavaScript

Satish Shriwas·Oct 24, 2026·3 min read
Original on Medium ↗

How can we find all DOM elements that match a specific CSS property and value without using querySelectorAll()?

The solution uses DOM traversal and recursion to visit every descendant and collect matching elements in an array.

Photo by Greg Rakozy on Unsplash

1. The Problem

Implement a function that accepts three arguments:

  • element: The root element to search from.
  • property: The CSS property to check.
  • value: The expected CSS value.

For example:

javascript

getElementsByStyle(document.body, 'font-size', '12px');

The function should return all descendants whose computed font size is 12px, excluding the root element itself.

2. Implementation

javascript

export default function getElementsByStyle(element, property, value) {
const result = [];
function traverse(node) {
for (const child of node.children) {
if (getComputedStyle(child).getPropertyValue(property) === value) {
result.push(child);
}
traverse(child);
}
}
traverse(element);
return result;
}

3. Understanding the Approach

  • node.children gives us the direct child elements.
  • getComputedStyle(child).getPropertyValue(property) gets the browser-computed value of the specified CSS property.
  • If the value matches, we add the child to result.
  • traverse(child) recursively searches that child's descendants.
  • Finally, we return the array of matching elements.

The recursive call is important because matching elements can be nested several levels deep.

4. Dry Run: Step-by-Step Execution

Consider the following HTML:

<div id="root">
<span style="font-size: 12px">Span</span>
<section>
<p style="font-size: 12px">Paragraph</p>
</section>
<blockquote style="font-size: 14px">Blockquote</blockquote>
</div>

We call:

javascript

getElementsByStyle(root, 'font-size', '12px');

Initial values:

javascript

result = []
property = 'font-size'
value = '12px'

Let’s trace the execution.

Step 1: Start with root

traverse(root) begins iterating over the root's children.

The first child is span.

Computed style:

javascript

getComputedStyle(span).getPropertyValue('font-size')
// "12px"

Comparison:

javascript

"12px" === "12px" // true

So, span is added to the result.

javascript

result = [span]

Next, traverse(span) is called. Since span has no children, the recursive call returns immediately.

Step 2: Visit section

The loop continues to the next child, section.

Its computed font size is assumed to be 16px in this example.

javascript

"16px" === "12px" // false

Nothing is added.

javascript

result = [span]

But we still call:

javascript

traverse(section)

This explores the children inside section.

Step 3: Visit the nested p

Inside section, the child is p.

javascript

getComputedStyle(p).getPropertyValue('font-size')
// "12px"

Comparison:

javascript

"12px" === "12px" // true

The paragraph is added.

javascript

result = [span, p]

Next, traverse(p) runs. Since p has no children, it returns immediately.

Control then returns to the loop inside traverse(root).

Step 4: Visit blockquote

The next child of root is blockquote.

javascript

getComputedStyle(blockquote).getPropertyValue('font-size')
// "14px"

Comparison:

javascript

"14px" === "12px" // false

The element is not added.

javascript

result = [span, p]

Since blockquote has no children, its recursive call returns immediately.

Step 5: Return the result

All descendants have now been checked.

javascript

return [span, p];

The final result contains the span and p elements, in document order.

5. Complexity Analysis

  • Time: O(n), where n is the number of descendant elements, assuming each style lookup takes constant time.
  • Recursive stack space: O(h), where h is the maximum depth of the DOM tree.
  • Result array: O(n) in the worst case if every descendant matches.

Implementing getElementsByStyle() in JavaScript was originally published in Modern Frontend on Medium, where people are continuing the conversation by highlighting and responding to this story.