--- title: NodeList.prototype.forEach() slug: Web/API/NodeList/forEach tags: - API - DOM - Liste - Méthodes - Noeuds translation_of: Web/API/NodeList/forEach ---
{{APIRef("DOM")}}
La méthode forEach()
de l'interface {{domxref("NodeList")}} appelle le rappel donné en paramètre une fois pour chaque paire de valeurs dans la liste, dans l'ordre d'insertion.
nodeList.forEach(callback[, thisArg]);
callback
currentValue
currentIndex
listObj
forEach()
est appliqué.thisArg
{{Optional_inline}}
callback
(rappel).{{jsxref('undefined')}} (indéfini).
Aucune.
var node = document.createElement("div"); var kid1 = document.createElement("p"); var kid2 = document.createTextNode("hey"); var kid3 = document.createElement("span"); node.appendChild(kid1); node.appendChild(kid2); node.appendChild(kid3); var list = node.childNodes; list.forEach( function(currentValue, currentIndex, listObj) { console.log(currentValue + ', ' + currentIndex + ', ' + this); }, 'myThisArg' );
résultat :
[object HTMLParagraphElement], 0, myThisArg [object Text], 1, myThisArg [object HTMLSpanElement], 2, myThisArg
Ce {{Glossary("Polyfill","polyfill")}} ajoute une compatibilité à tous les navigateurs prenant en charge ES5 :
if (window.NodeList && !NodeList.prototype.forEach) { NodeList.prototype.forEach = function (callback, thisArg) { thisArg = thisArg || window; for (var i = 0; i < this.length; i++) { callback.call(thisArg, this[i], i, this); } }; }
Spécification | Statut | Commentaire |
---|---|---|
{{SpecName('DOM WHATWG', '#interface-nodelist', 'NodeList')}} | {{ Spec2('DOM WHATWG') }} | Définit NodeList comme iterable<Node> (noeud itérable) |
{{SpecName("WebIDL", "#es-forEach", "forEach")}} | {{Spec2("WebIDL")}} | Définit forEach sur les déclarations iterable (itératives) |
{{Compat("api.NodeList.forEach")}}