Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get all ids on page

Tags:

jquery

Is it possible to get an array consisting of all id's on a page with jQuery?

like image 203
Brian Avatar asked Mar 02 '10 22:03

Brian


People also ask

How to get all ids in 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.

How to access div id in jQuery?

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.

How do I find div id?

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?

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.


2 Answers

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.

like image 111
Nick Craver Avatar answered Oct 21 '22 08:10

Nick Craver


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/

like image 34
karim79 Avatar answered Oct 21 '22 08:10

karim79