getting the children

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wim Roffal

    getting the children

    I am having a <div> with <a>s and <div>s inside it.

    I am trying to read in all the elements in the right order.

    With mydiv.getElemen tsByTagName("a" ) I can get the elements of a specific
    tag like "a", but I want all types.

    I tried getChildNodes but that doesn't seem to be widely supported.
    childNodes[] seemed to do something but I couldn't get it working when I
    wanted to browse through the elements.

    What is the best way to do this?

    A related question. If I have an element, how can I see which type (a, div,
    input,etc) it is?

    Thanks,

    Wim


  • Michael Winter

    #2
    Re: getting the children

    On Sat, 18 Sep 2004 18:39:13 +0200, Wim Roffal
    <wimroffelpleas e@nospamm-planet.nl> wrote:

    [snip]
    [color=blue]
    > I tried getChildNodes but that doesn't seem to be widely supported.[/color]

    Probably because it doesn't exist. It's not part of Microsoft's DOM, or
    the standardised W3C DOM (unless it's in Level 3).
    [color=blue]
    > childNodes[] seemed to do something but I couldn't get it working when I
    > wanted to browse through the elements.
    >
    > What is the best way to do this?[/color]

    Mind showing what you were trying to do? The childNodes collection is your
    best bet.
    [color=blue]
    > A related question. If I have an element, how can I see which type (a,
    > div, input,etc) it is?[/color]

    Use the nodeName property. This will return a case-sensitive string with
    the element name. In HTML, the string will always be in uppercase.

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Fred Oz

      #3
      Re: getting the children

      Michael Winter wrote:[color=blue]
      > On Sat, 18 Sep 2004 18:39:13 +0200, Wim Roffal
      > <wimroffelpleas e@nospamm-planet.nl> wrote:
      >
      > [snip]
      >[color=green]
      >> I tried getChildNodes but that doesn't seem to be widely supported.[/color]
      >
      >
      > Probably because it doesn't exist. It's not part of Microsoft's DOM, or
      > the standardised W3C DOM (unless it's in Level 3).
      >[color=green]
      >> childNodes[] seemed to do something but I couldn't get it working when
      >> I wanted to browse through the elements.
      >>
      >> What is the best way to do this?[/color]
      >
      >
      > Mind showing what you were trying to do? The childNodes collection is
      > your best bet.
      >[/color]

      To help you out, childNodes returns a collection,
      you can do something like:

      var a = document.getEle mentById('aDiv' ).childNodes;
      for (var i=0; i<a.length; ++i) {
      // do something to each child
      }

      Other useful methods are firstChild and nextSibling - which is
      really useful if you start at some unknown point in a DOM and
      just want the next sibling. Say you want to find the next one
      after "a" above without having to find the parentNode, work out
      the index of "a", then get the next one. It's all
      done by "var b = a.nextSibling".
      [color=blue][color=green]
      >> A related question. If I have an element, how can I see which type
      >> (a, div, input,etc) it is?[/color]
      >
      >
      > Use the nodeName property. This will return a case-sensitive string
      > with the element name. In HTML, the string will always be in uppercase.[/color]

      There is also nodeType, but different browsers seem to put
      different node types in some places so it may not be that
      useful for you.


      Cheers, Fred.

      Comment

      • Michael Winter

        #4
        Re: getting the children

        On Sun, 19 Sep 2004 16:02:50 +1000, Fred Oz <ozfred@iinet.n et.auau> wrote:

        [snip]
        [color=blue][color=green]
        >> On Sat, 18 Sep 2004 18:39:13 +0200, Wim Roffal
        >> <wimroffelpleas e@nospamm-planet.nl> wrote:[/color][/color]

        [snip]
        [color=blue][color=green][color=darkred]
        >>> A related question. If I have an element, how can I see which type
        >>> (a, div, input,etc) it is?[/color][/color][/color]

        [snip]
        [color=blue]
        > There is also nodeType, but different browsers seem to put
        > different node types in some places so it may not be that
        > useful for you.[/color]

        The nodeType property doesn't return the information the OP wanted,
        though. It returns quite literally the type: element, comment, DOCTYPE,
        text, etc. However, that information does prove very useful when iterating
        through the tree when looking for generic types. If you're looking for a
        specific element, such as an INPUT, there isn't much need for it.

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        Working...