Mateusz Kowalski
Mateusz Kowalski

Reputation: 17

Get all existing html tags with javascript

I'm wondering if its possible to get all existing html tags(body, head, div, ul etc.) with javascript. By all existing i dont mean all on the page, but all valid. Is there a function that does that?

Also if there isn't - Is it somewhere in Firefox or Chrome files?

Upvotes: 0

Views: 729

Answers (3)

brains_at_work
brains_at_work

Reputation: 251

Different approach not accessing the DOM, since I'm still not sure what the aim is: Take the document as a string and look for "<"and ">" with a regular expression? Shall I elaborate on that?

Upvotes: 0

Thijs Riezebeek
Thijs Riezebeek

Reputation: 1812

You could write a function that retrieves all the child elements of the <html> element. This would allow you to get all of the html elements on the page.

function getAllChildElements (start) {
    var children = start.childNodes;
    var elements = children;

    for (var i = 0; i < children.length; i++) {
        elements.concat(getAllChildElements(children[i]);
    }

    return elements;
}

var all_elements = getAllChildElements(document.querySelector('html'));

Upvotes: 0

Ramanlfc
Ramanlfc

Reputation: 8354

if you want all tags this should do:

document.querySelectorAll("*");

if you want html child tags :

document.querySelector("html").children;

Upvotes: 1

Related Questions