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

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -