www.webdeveloper.com
Results 1 to 2 of 2

Thread: Help with simple JavaScript question...

  1. #1
    Join Date
    Oct 2009
    Posts
    1

    Help with simple JavaScript question...

    Hey everyone, I'm a JavaScript beginner and I can't seem to figure out something really simple. I am trying to access an element located inside of another element. Here is an example of what I mean:

    I have a <ul> with an id of "topics" and within that <ul> I need to access all of the <h3>'s that are inside of it.

    HTML Example:
    <ul id = "topics">
    <h3>Blah</h3>
    <li>junk</li>
    <li>junk</li>
    <h3MoreBlah</h3>
    <li>morejunk</li>
    </ul>

    How do I access the <h3>'s to change their attributes?

    Hopefully this makes sense, again, newb here.

    Thanks in advance!

  2. #2
    Join Date
    Sep 2009
    Posts
    52
    The following example sets the color of the H3s inside 'topics' to red. Only tested in Firefox.

    PHP Code:
    <html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    script type="text/javascript">
    window.onload = function() {
      var 
    topics document.getElementById('topics');
      var 
    headings topics.getElementsByTagName('h3');
      var 
    len headings.length;
      for (var 
    i=0i<leni++) {
        var 
    heading headings[i];
        
    heading.style.color 'red';
      } 
    }
    </script>
    </head>
    <body>
    <ul id="topics">
    <h3>Blah</h3>
    <li>junk</li>
    <li>junk</li>
    <h3>MoreBlah</h3>
    <li>morejunk</li>
    </ul>
    <h3>not included</h3>
    </body>
    </html> 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles