SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    var tagsOpen=new Array();
    tagsOpen[]='<h1 class="h">';
    tagsOpen[]='<p>';
    tagsOpen[]='<strong class="w">';
    tagsOpen[]='<p>';
    tagsOpen[]='<span style="color: red">';
    tagsOpen[]='<span style="color: blue">';
    tagsOpen[]='<span style="color: green">';
    That array should be dynamically created from this:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Title</title>
    </head>
    <body>
    
    <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>
    </div>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    
    var obj=document.getElementById('content');
    if(obj) alert(obj.innerHTML);
    
    /*]]>*/
    </script>
    
    </body>
    </html>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Let's start off quite simply.

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

    Code javascript:
    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

    Code javascript:
    var bodyElements = document.body.getElementsByTagName('*');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •