jQuery select attributes into an array

****edit****

ok the glove has been thrown down…

var elements = (document.getElementsByTagName('li'));
var vals = [];
for(var i=0;typeof(elements[i])!='undefined';vals.push(elements[i++].getAttribute('value')));

no library 3 lines of code…

epicly faster

enter image description here


var myVals = [];
$('li','ul').each(function(){
  myVals.push($(this).attr('value'));
});

and using jquery’s map function…

var myVals = [];
$('li','ul').map(function(){
  myVals.push($(this).attr('value'));
});

and they are both equally as fast..
http://jsperf.com/testing-stuff
enter image description here

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)