How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Replace jQuery’s .each() with Vanilla JavaScript
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Replace jQuery’s .each() with Vanilla JavaScript
Web Development

How to Replace jQuery’s .each() with Vanilla JavaScript

how7o
By how7o
Last updated: May 23, 2026
4 Min Read
Replace jQuery .each with vanilla JavaScript loops
SHARE

To replace jQuery’s .each() with vanilla JavaScript, use document.querySelectorAll(selector) to get the elements, then iterate with forEach, for...of, or a plain for loop. The modern equivalent of $('.list-item').each(fn) is one line: document.querySelectorAll('.list-item').forEach(fn).

Contents
  • The jQuery version
  • Vanilla equivalents
  • Getting the index
  • Early exit (replacement for return false)
  • Operating on each element
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 in Chrome 124, Firefox 125, Safari 17. Originally published 2022-10-06, rewritten and updated 2026-05-17.

The jQuery version

$('.list-item').each(function () {
    // 'this' is the DOM element
    console.log(this.textContent);
});

Vanilla equivalents

// forEach (most idiomatic for simple iteration)
document.querySelectorAll('.list-item').forEach((el) => {
    console.log(el.textContent);
});

// for...of (supports break and continue)
for (const el of document.querySelectorAll('.list-item')) {
    console.log(el.textContent);
}

// Classic for loop (most flexible)
const elems = document.querySelectorAll('.list-item');
for (let i = 0; i < elems.length; i++) {
    console.log(elems[i].textContent);
}
Replace jQuery .each() with vanilla JS — forEach, for...of, classic for loop, getting index

Getting the index

// forEach — index is the second argument
document.querySelectorAll('.list-item').forEach((el, i) => {
    el.dataset.index = i;
});

// for...of — use entries()
for (const [i, el] of document.querySelectorAll('.list-item').entries()) {
    el.dataset.index = i;
}

// Classic — use the counter
const elems = document.querySelectorAll('.list-item');
for (let i = 0; i < elems.length; i++) {
    elems[i].dataset.index = i;
}

Early exit (replacement for return false)

// for...of with break
for (const el of document.querySelectorAll('.list-item')) {
    if (el.classList.contains('stop')) break;
    // ...
}

// Array.some() — stops at the first truthy return
[...document.querySelectorAll('.list-item')].some((el) => {
    if (el.classList.contains('stop')) return true;   // break
    return false;
});

forEach always runs to completion — no way to break out. Use for...of or some() when you need early termination.

Operating on each element

document.querySelectorAll('.list-item').forEach((el) => {
    el.classList.toggle('seen');
    el.dataset.processed = '1';
    el.textContent = el.textContent.trim();
});

Most jQuery method chains map directly to native DOM API methods — $(el).addClass(x) → el.classList.add(x), $(el).attr('y', z) → el.setAttribute('y', z), $(el).text(t) → el.textContent = t.

Frequently asked questions

Why doesn’t NodeList.forEach work in older browsers?

It does on every actually-modern browser (Chrome 51+, Firefox 50+, Safari 10+, Edge 16+ — released in 2016/2017). If you need to support IE11, convert the NodeList first: Array.from(nodes).forEach(...) or [...nodes].forEach(...). HTMLCollection (e.g. from getElementsByClassName) doesn’t have forEach directly — wrap it the same way.

What’s the difference between for, forEach, and for...of?

All iterate over the same elements. for with an index is the most flexible (you can break, modify the counter, skip steps). forEach reads cleanest for simple iteration but can’t break. for...of reads almost as cleanly as forEach but supports break and continue. For new code, for...of is the closest replacement for jQuery’s .each().

How do I get the index in forEach or for...of?

forEach((element, index) => ...) passes the index as the second argument. With for...of, use entries(): for (const [i, el] of nodes.entries()) { ... }. Or, if the collection is an array, Array.from(nodes).entries().

Can I break out of forEach like jQuery’s return false?

No — forEach always runs to completion. To stop early, use for, for...of, some() (returns true on first match and stops), or find() (returns the matching element). Each has a different return shape — pick the one whose return value you actually want.

Related guides

  • How to Get the Index in a jQuery .each() Loop
  • How to Break Out of a jQuery .each() Loop
  • How to Get N Elements from an Array in JavaScript

References

MDN document.querySelectorAll(): developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll. MDN NodeList.forEach(): developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach. MDN for...of: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for…of.

TAGGED:domJavaScriptjQuery

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Trigger a function when DataTables loads data via Ajax How to Trigger a Function When DataTables Loads Data
Next Article Switch from LiteSpeed to Apache in WHM/cPanel How to Switch from LiteSpeed to Apache in WHM/cPanel
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Laravel Blade @foreach vs @forelse comparison
Web Development

Laravel Blade: Difference Between @foreach and @forelse

4 Min Read
Laravel Eloquent group by count — Book::groupBy('author') query with bar-chart aggregate icon
Web Development

How to Count Records Grouped By a Column in Laravel Eloquent

7 Min Read
Enforce a minimum phone-number length in WooCommerce checkout
Web Development

How to Set a Minimum Length for Phone Numbers in WooCommerce

6 Min Read
Remove unwanted characters from a PHP string with regex
Web Development

How to Remove Unwanted Characters from a String in PHP

5 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?