I’m running a forEach
loop using document.querySelectorAll()
, and I need to get the index of each node. What would be the best way to do this?
function queryFunc(query, func){
document.querySelectorAll(query).forEach(func);
};
queryFunc(".fooBar", function(e){
//I need to return the index of the cell that called the function.
});
3
Answers
index
is always the second parameter in aforEach
loop.Based on your edit, here is how you can call the
index
argument from the external function.You can use foreach second argument to obtain cell index in the array:
If you mean the cellIndex of an HTMLTableCellElement, you can simply use the
cellIndex
property: