I would like to be able to use javascript to find every id (or name) for every object in an html document so that they can be printed at the bottom of the page.
To under
If you're doing your development using a fairly modern browser, you can use querySelectorAll()
, then use Array.prototype.forEach
to iterate the collection.
var ids = document.querySelectorAll('[id]');
Array.prototype.forEach.call( ids, function( el, i ) {
// "el" is your element
console.log( el.id ); // log the ID
});
If you want an Array of IDs, then use Array.prototype.map
:
var arr = Array.prototype.map.call( ids, function( el, i ) {
return el.id;
});