Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Iterating through HTML using the DOM

    I am trying to iterate through the DOM of my document using javascript, but I am unsure about how to start.

    I assume my js file should be something like this, but I'm just not sure how to get it working... can someone point me in the right direction?

    Code:
    var all = document.getElementsByTagName("*");
    
    for (var i=0, max=all.length; i < max; i++) {
         // Do something with the element here
    }
    Below is my HTML code.. very basic coding as you can tell.
    Code:
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="lab2.css" />
    </head>
    
    <body>
    
    <h1> First Song </h1>
    
       <ul>
          <p class="small"> Make You Wonna </p>
          Jakebeatz & Jay-Rock
          <p class="italic"> Power to the B-Boyz </p>
          <img src="make you wonna.jpg" />
          <p class="scale"> 9/29/91 </p>
          <p class="scale"><li> Electronic </li></p>
          <a href="www.jay-roc.com" > Jay Roc </a>
       </ul>
    
    <h2> Second Song </h2>
       <ul>
          <p class="small"><Good Life </p>
          Onerepublic
          <p class="italic"> Waking Up </p>
          <img src="good life.jpg" />
          <p class="scale"> 9/29/91 </p>
          <p class="scale"><li> Pop </li></p>
          <a href = "www.onerepublic.net" > one </a>
       </ul>
    
    <h3> Third Song </h3>
       <ul>
          <p class="small"> Fall For You </p>
          Secondhand Serenade
          <p class="italic"> A Twist in My Story </p>
          <img src="fall for you.jpg" />
          <p class="scale"> 9/29/91 </p>
          <p class="scale"><li> Alternative </li></p>
          <a href = "www.secondhandserenade.net" > secondhand </a>
       </ul>
    
    <h4> Fourth Song </h4>
       <ul>
          <p class="small"> Moves Like Jagger </p>
          Maroon 5
          <p class="italic"> Hands All Over </p>
          <img src="m.jpg </li>
          <p class="scale"> 9/29/91 </p>
          <p class="scale"><li> Pop </li></p>
          <a href = "www.maroon5.com" > maroon </a>
       <ul>
    
    
    <script src="lab3.js">
    </script>
    
    </body>
    </html>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,901
    Thanks
    56
    Thanked 543 Times in 540 Posts
    Looks ok as far as you've got. What's the problem?


  •  

    Posting Permissions

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