Get a list of elements inside a tag

Discussion in 'Javascript' started by Juliano.net, Jul 1, 2006.

  1. Juliano.net

    Juliano.net Guest

    How can I get a list of elements inside a tag and when I find one
    element of a type that I choose I can change its ID and CLASS
    attribute?

    Ex:
    <div id="content"> <!-- I want to get the list of elements inside the
    DIV with ID content -->
    <a href="#">My link</a>
    <b>Some trash</b>
    <div> <!-- The first DIV element I find, will be used and I'll change
    it's ID -->
    <div id="innerdiv1">
    </div>
    <div id="innerdiv2">
    </div>
    </div>
    </div>

    How can I do this?
     
    Juliano.net, Jul 1, 2006
    #1
    1. Advertising

  2. "Juliano.net" <> writes:

    > How can I get a list of elements inside a tag and when I find one
    > element of a type that I choose I can change its ID and CLASS
    > attribute?


    var contentDiv = document.getElementById("content");

    // do something to first div element:
    var containedDivElements = contentDiv.getElementsByTagName("div");
    var firstDiv = containedDivElements[0];

    firstDiv.id = "changedId";
    firstDiv.className = "changedClass";

    // run through all contained elements:
    var allContainedElements = contentDiv.getElementsByTagName("*");
    for (var i = 0; i < allContainedElements.length; i++) {
    var elem = allContainedElements;
    // do something with contained elem
    }


    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jul 1, 2006
    #2
    1. Advertising

  3. Juliano.net

    Juliano.net Guest

    Lasse, how can I get the name and values for all attributes of a tag?

    And is there any JavaScript variables watcher (debugger) that allows
    seeing the variables values changing? Like a Delphi or Visual Basic
    debugger.

    Lasse Reichstein Nielsen wrote:
    > "Juliano.net" <> writes:
    >
    > > How can I get a list of elements inside a tag and when I find one
    > > element of a type that I choose I can change its ID and CLASS
    > > attribute?

    >
    > var contentDiv = document.getElementById("content");
    >
    > // do something to first div element:
    > var containedDivElements = contentDiv.getElementsByTagName("div");
    > var firstDiv = containedDivElements[0];
    >
    > firstDiv.id = "changedId";
    > firstDiv.className = "changedClass";
    >
    > // run through all contained elements:
    > var allContainedElements = contentDiv.getElementsByTagName("*");
    > for (var i = 0; i < allContainedElements.length; i++) {
    > var elem = allContainedElements;
    > // do something with contained elem
    > }
    >
    >
    > /L
    > --
    > Lasse Reichstein Nielsen -
    > DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    > 'Faith without judgement merely degrades the spirit divine.'
     
    Juliano.net, Jul 3, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kishan bisht

    struts tag inside a tag

    kishan bisht, Jul 8, 2003, in forum: Java
    Replies:
    1
    Views:
    1,501
    Wendy S
    Jul 9, 2003
  2. shahbaz
    Replies:
    0
    Views:
    924
    shahbaz
    Oct 27, 2003
  3. shruds
    Replies:
    1
    Views:
    872
    John C. Bollinger
    Jan 27, 2006
  4. Adam Hartshorne
    Replies:
    2
    Views:
    381
    Nitin Motgi
    Jan 27, 2006
  5. Kenneth

    access elements inside <div> tag

    Kenneth, May 6, 2005, in forum: Javascript
    Replies:
    1
    Views:
    88
Loading...

Share This Page