Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all html tags with Javascript

Does anybody knows how can I get all the HTML tags that exist in a page? I need to get only the tags without their IDs or other attributes, and create a kind of tree-structure of them. Prefer to do that with Javascript or JQuery.

For example, This HTML code:

<html>
  <head>
    <title>
      Example Page 
  </title>
  </head>
  < body>
    <h1 style="somestyle">
      Blabla
  </h1>
  <div id="id">
    <table id="formid">
      <tr>
        <td>
        </td>
      </tr>
      </table>
  </div>
  </body>
</html>

should return return:

html
head
title
body
h1
div
table
tr
td

like image 232
Gal Avatar asked Mar 02 '15 15:03

Gal


Video Answer


1 Answers

You can pass a * to getElementsByTagName() so that it will return all elements in a page:

var all = document.getElementsByTagName("*");

for (var i=0, max=all.length; i < max; i++) {
     // Do something with the element here
}
like image 148
Jordi Castilla Avatar answered Sep 28 '22 22:09

Jordi Castilla