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
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

Format a PHP number with leading zeros — sprintf and str_pad
Web Development

How to Format a Number with Leading Zeros in PHP

4 Min Read
MySQL remove string from column — REPLACE and REGEXP_REPLACE patterns
Web Development

How to Remove a Specific String from a Column in MySQL

6 Min Read
Securely hash passwords in PHP with password_hash
Web Development

Securely Hash Passwords in PHP (password_hash, Argon2id)

6 Min Read
Laravel delete file from public folder — File::delete with public_path
Web Development

How to Delete Files from the Public Folder in Laravel

7 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?