Is it possible to get an array consisting of all id's on a page with jQuery?
You can use jQuery attr() and prop() methods to retrieve element ids. To retrieve ids of multiple <div> elements, you will have to iterate or loop through each element on your web page using jQuery . each() method.
Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to get or set the ID attribute value of an element. The following example will display the ID of the DIV element in an alert box on button click.
In JavaScript, you can use getElementById() fucntion to get any prefer HTML element by providing their tag id. Here is a HTML example to show the use of getElementById() function to get the DIV tag id, and use InnerHTML() function to change the text dynamically.
Which jQuery statement selects the DOM element with an id of testid'? Calling jQuery() (or $() ) with an id selector as its argument will return a jQuery object containing a collection of either zero or one DOM element.
You could do this:
var ids = new Array();
$('[id]').each(function() { //Get elements that have an id=
ids.push($(this).attr("id")); //add id to array
});
//do something with ids array
One note I saw testing this, the FireBug console counts as one, if that's enabled just be aware.
var ids = $('*[id]').map(function() {
return this.id;
}).get();
The .map() method is particularly useful for getting or setting the value of a collection of elements.
http://api.jquery.com/map/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With