Gist a9a28ae7918d9806bc45f4b440a0bca6
✓ Published0🌍 Public
KKopylovVitalik
Last edited Apr 15, 2020
Created on Apr 15, 2020
This example demonstrates a practical vanilla JavaScript replacement for jQuery’s `index()` and `eq()` methods, showing how to locate an element’s position among its siblings and select a specific node by index. The code uses `document.querySelectorAll('.class')` to fetch all elements with that class, then sets the text content of the first matching element to `1254` via `[0].textContent`, mirroring `eq(0)`. It also defines a helper function, `getNodeindex`, that spreads `elm.parentNode.children` into an array and calls `indexOf(elm)` to return the element’s zero-based index, replicating `index()`. The example highlights modern DOM traversal techniques without external libraries.
AI-generated description