I want to manipulate a collection of DOM elements..
Here’s an Example:
HTML:
<div class="items">
<p class="item">Here's some text</p>
<p class="item">Here's another text</p>
<p class="item">Here's some another text</p>
</div>
JS:
const items = document.querySelector(".item")
// This is how I do this vanilla JS way
items.forEach(item => {
item.style.color = "#333"
})
// I want to achieve something like jQuery where we don't have to use loop
$(".item").css("color", "#333")
Thanks in advance!
I tried finding solutions but could not find the best solution.
2
Answers
You can monkey patch the
NodeList
prototype or write an alike function/class/factory without monkey patching (like jQuery for example):Here is one version that does not use a loop and it is really fast too: