Search code examples
javascriptjquerydomtree-traversal

Find all elements with an 'id' attribute on the DOM


Simple. I want to traverse the DOM and find all elements that have an id attribute. Can someone help me with a js and/or jquery script


Solution

  • You can query all the elements with an id attribute by using the following jQuery selector:

    $("[id]")
    

    You can also just use plain JavaScript using querySelectorAll:

    document.querySelectorAll("[id]")
    

    If you want to find non-empty id attributes, use this selector instead:

    '[id]:not([id=""])'