className (ie vs moz)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tgh003@gmail.com

    className (ie vs moz)

    I am trying to retrieve a the class name of a parentnode.

    In Firefox this works:
    document.getEle mentById('foo') .parentNode.cla ssName

    returns the class name of the parent element. In IE it doesnt work.

    What is the correct syntax for IE?

    Any help is much appreciated.
    Thx.

  • RobG

    #2
    Re: className (ie vs moz)

    tgh003@gmail.co m wrote:[color=blue]
    > I am trying to retrieve a the class name of a parentnode.
    >
    > In Firefox this works:
    > document.getEle mentById('foo') .parentNode.cla ssName
    >
    > returns the class name of the parent element. In IE it doesnt work.
    >
    > What is the correct syntax for IE?
    >[/color]

    Can't tell what your issue is. The following works for me in IE 6
    and Firefox:

    <div class="blah"><s pan id="foo">foo</span></div>

    <script type="text/javascript">
    alert(document. getElementById( 'foo').parentNo de.className);
    </script>



    --
    Rob

    Comment

    • RobB

      #3
      Re: className (ie vs moz)

      tgh...@gmail.co m wrote:[color=blue]
      > I am trying to retrieve a the class name of a parentnode.
      >
      > In Firefox this works:
      > document.getEle mentById('foo') .parentNode.cla ssName
      >
      > returns the class name of the parent element. In IE it doesnt work.
      >
      > What is the correct syntax for IE?
      >
      > Any help is much appreciated.
      > Thx.[/color]

      function getAncestorClas s(node)
      {
      var cN;
      while (node = node.parentNode )
      if (cN = node.className)
      return cN;
      return null;
      }

      getAncestorClas s(document.getE lementById('foo '));

      Comment

      • Matt Kruse

        #4
        Re: className (ie vs moz)

        tgh003@gmail.co m wrote:[color=blue]
        > In Firefox this works:
        > document.getEle mentById('foo') .parentNode.cla ssName
        > returns the class name of the parent element. In IE it doesnt work.[/color]

        Most likely, the 'parentNode' of the element is not the same in each
        browser.

        Try
        alert(document. getElementById( 'foo').parentNo de.tagName)
        or
        alert(document. getElementById( 'foo').parentNo de.outerHTML)

        to see what the browser is really pointing to.

        --
        Matt Kruse



        Comment

        • Martin Honnen

          #5
          Re: className (ie vs moz)



          tgh003@gmail.co m wrote:
          [color=blue]
          > I am trying to retrieve a the class name of a parentnode.
          >
          > In Firefox this works:
          > document.getEle mentById('foo') .parentNode.cla ssName
          >
          > returns the class name of the parent element. In IE it doesnt work.[/color]

          Element nodes have a className property in IE 4 and later, and nodes
          have a parentNode property in IE 5 and later. document.getEle mentById is
          supported in IE 5 and later too so your expression should work with IE 5
          and later.
          When you say "it doesn't work in IE" what exactly happens, do you get a
          script error, if so which one exactly?

          --

          Martin Honnen

          Comment

          • tgh003@gmail.com

            #6
            Re: className (ie vs moz)

            thanks for the replies

            your right it should work but the value is nothing. like null. I dont
            even get undefined but just blank.

            I should say I am using the createElement to create the nodes and use
            ..className = "foo" to set the class

            Displays properly but i cannot retrieve the value. strange.

            Comment

            • tgh003@gmail.com

              #7
              Re: className (ie vs moz)

              Matt,

              You were exacly right. IE has a different parentNode than Firefox.
              What a POS - I am really starting to hate javascript because of these
              issues.

              Thx!

              Comment

              • Matt Kruse

                #8
                Re: className (ie vs moz)

                tgh003@gmail.co m wrote:[color=blue]
                > You were exacly right. IE has a different parentNode than Firefox.
                > What a POS - I am really starting to hate javascript because of these
                > issues.[/color]

                Don't hate javascript - it's just the language you're using to access the
                browser's DOM.
                DOMs can be different. You just have to program appropriately.

                If you have an <INPUT> and you expect its parent to be a <TD>, don't rely on
                that. Instead, walk up the parentNode chain and stop when you get to a <TD>
                element. That way, you won't be dependent on a particular browser's decision
                to insert assumed objects or rearrange when you didn't expect it to.

                --
                Matt Kruse



                Comment

                • RobG

                  #9
                  Re: className (ie vs moz)

                  tgh003@gmail.co m wrote:[color=blue]
                  > Matt,
                  >
                  > You were exacly right. IE has a different parentNode than Firefox.
                  > What a POS - I am really starting to hate javascript because of these
                  > issues.[/color]

                  Don't blame JavaScript, it is just a scripting language - your issue
                  is with the different way various browsers implement the DOM.

                  It is often risky to depend on parentNode/childNode relationships and
                  frequently search or iterative techniques are required to ensure the
                  'correct' descendant/ancestor is recognised, e.g.

                  <table>
                  <tr>
                  <td onclick="alert( this.parentNode .parentNode)">c lick me</td>
                  </tr>
                  </table>

                  Will report 'TBODY', not 'TABLE'. If the table was the target, it
                  may be better to use a function that goes up the parents until the
                  table is found.

                  Try this in Firefox and then IE:

                  <script type="text/javascript">
                  function doClick(x){
                  var s = x.nextSibling;
                  alert('nextSibl ing is : ' + ((s)? s.nodeName : 'nothing' ));
                  }
                  </script>
                  <ul>
                  <li onclick="doClic k(this)">click me</li>
                  <li onclick="doClic k(this)">click me</li>
                  </ul>


                  Try posting a bit of the HTML you are having trouble with.

                  --
                  Rob

                  Comment

                  Working...