Skip to content

Instantly share code, notes, and snippets.

@Paratron
Created August 2, 2012 10:33
Show Gist options
  • Save Paratron/3236143 to your computer and use it in GitHub Desktop.
Save Paratron/3236143 to your computer and use it in GitHub Desktop.
Get id attribute of all elements with a certain class.
//Without jQuery
var elements = document.getElementsByClassName('clicked'), //Be careful: won't work in IE 6,7,8
ids = [];
for(var i = 0; i < elements.length; i++){
ids.push(elements[i].id);
}
console.log(ids);
//==============================================================
//With jQuery
var ids = [];
$.each($('.clicked'), function(){
ids.push($(this).attr('id'));
});
console.log(ids);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment