How to view a list of all id used on the page in the html code?

    2 answers 2

    You can get all the elements that have an id, and display a list:

    var ids = document.querySelectorAll('[id]'); Array.prototype.forEach.call( ids, function( el, i ) { console.log( el.id ); // log the ID }); 

    If you need an array of id

     var ids = document.querySelectorAll('[id]'); var arr = Array.prototype.map.call( ids, function( el, i ) { return el.id; }); 

    PS took from the English version , the author of the answer @ user113716

    • I myself need to look at the id, not the elements to choose. - user208916
    • @Khipster go over the selected collection and collect them into an array) - Alexey Shimansky
    • @Khipster, in the browser console you can execute the code, I added the output of the list id. - Evgenii Izhboldin 8:40 pm

    With jquery, I would write something like this:

     var ids = $('[id]').map((i, el) => el.id); 

    In principle, it should work like this:

     var ids = [].map.call(document.querySelectorAll('[id]'), el => el.id); 
    • there is no jquery tag in the question ;-) - Alexey Shimansky
    • Added, but the meaning is the same. - YuS
    • only the code is incorrect) document.querySelectorAll('[id!=""]') will complain that the selector is invalid ... and even if the selector were valid, it will continue to swear that the map is not a function - Alexey Shimansky
    • But in the tags were google chrome , вэб-дизайн and вэб-программирование . I mean, relevance is dubious here. - YuS
    • @ Alexey Shimansky, I agree with the comments. - YuS