JavaScript: Put all opening HTML tags in array

I want to place all opening tags in an array.

In the example below, the following array should be created:

var tagsOpen=new Array();
tagsOpen[]='<h1 class="h">';
tagsOpen[]='<strong class="w">';
tagsOpen[]='<span style="color: red">';
tagsOpen[]='<span style="color: blue">';
tagsOpen[]='<span style="color: green">';

That array should be dynamically created from this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">

<div id="content">
<h1 class="h">Heading</h1>
<p>Hello <strong class="w">World</strong>.</p>
<p><span style="color: red">red</span> <span style="color: blue">blue</span>  <span style="color: green">green</span></p>

<script type="text/javascript">

var obj=document.getElementById('content');
if(obj) alert(obj.innerHTML);



Let’s start off quite simply.

getElementsByTagName allows you to get all elements by using the * parameter.

var allElements = document.getElementsByTagName('*');

You can then use the nodeName of each element to get the name of each element.

if you only want the elements inside the body, you can use

var bodyElements = document.body.getElementsByTagName('*');