Getting ID of all elements of a certain class into an array in javascript -
i read this question, , accepted answer provided:
var ids = $('.cookie').map(function(index) { // callback function called once each matching element return this.id; }); how can above done in pure javascript?
i'd suggest following:
// using function.prototype.call() apply // array.prototype.map() array-like nodelist returned // document.queryselectorall(): var elementids = array.prototype.map.call(document.queryselectorall('.cookie'), function (cookie) { // first argument anonymous function ('cookie') // array element of array on we're iterating, // , here dom node. // here, return id property of current node: return cookie.id; }); references:
Comments
Post a Comment