insertBefore (oNewNode [, oChildNode]) problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Basch

    insertBefore (oNewNode [, oChildNode]) problems

    Can anyone tell me why I cant insertBefore() objects that are selected
    using getElementById( )?

    <html>
    <head>
    </head>

    <body>
    <p id="heading1"> </p>

    <script language="JavaS cript">

    var insultObj = document.create TextNode("Could you *be* any dumber");
    document.getEle mentById("headi ng1").insertBef ore(insultObj);

    var pObj = document.getEle mentById("headi ng1");
    alert (pObj.childNode s[1].nodeName);
    </script>

    </body>
    </html>

    Thanks,
    Derek Basch

  • Derek Basch

    #2
    Re: insertBefore (oNewNode [, oChildNode]) problems

    I should have also added that this is the error that I get:

    Error: uncaught exception: [Exception... "Not enough arguments
    [nsIDOMText.inse rtBefore]" nsresult: "0x80570001
    (NS_ERROR_XPC_N OT_ENOUGH_ARGS) " location: "JS frame ::
    http://172.20.0.70/evaluation_framework/test.htm :: <TOP_LEVEL> :: line
    12" data: no]

    Sorry about that.

    Comment

    • RobB

      #3
      Re: insertBefore (oNewNode [, oChildNode]) problems

      Derek Basch wrote:[color=blue]
      > I should have also added that this is the error that I get:
      >
      > Error: uncaught exception: [Exception... "Not enough arguments
      > [nsIDOMText.inse rtBefore]" nsresult: "0x80570001
      > (NS_ERROR_XPC_N OT_ENOUGH_ARGS) " location: "JS frame ::
      > http://172.20.0.70/evaluation_framework/test.htm :: <TOP_LEVEL> ::[/color]
      line[color=blue]
      > 12" data: no]
      >
      > Sorry about that.[/color]

      Pretty much answers your Q...Object.inse rtBefore() takes two arguments,
      the child node being inserted, and the (existing) sibling node you want
      to follow it. It's invoked, naturally, on the parent node of both
      children.

      More to the point: what exactly are you trying to do?

      Comment

      • Derek Basch

        #4
        Re: insertBefore (oNewNode [, oChildNode]) problems

        RobB wrote:[color=blue]
        > Pretty much answers your Q...Object.inse rtBefore() takes two[/color]
        arguments,[color=blue]
        > the child node being inserted, and the (existing) sibling node you[/color]
        want[color=blue]
        > to follow it. It's invoked, naturally, on the parent node of both
        > children.[/color]

        Ahhhh! It is invoked on the parent node. That is what I wasn't groking.
        I was thinking the method was invoked on the node that you wanted to
        "insert" the newChild "before".

        Correctly, it is invoked on the parent and you pass the newChild and
        and refChild parameters. "Reference CHILD" being the key point here ;).

        -------------------------[color=blue]
        >From W3 DOM2:[/color]

        insertBefore
        Inserts the node newChild before the existing child node refChild.
        If refChild is null, insert newChild at the end of the list of
        children.
        .....
        Parameters
        newChild of type Node
        The node to insert.
        refChild of type Node
        The reference node, i.e., the node before which the new node
        must be inserted.
        -------------------------

        According to the W3 DOM the refChild parameter can be null if I don't
        care about where my newChild is inserted. So I made refChild a null
        keyword and it works great now. I guess the absence of a null keyword
        is not equivalent to the presence of a null keyword. Thanks for the
        help!

        <html>
        <head>
        </head>
        <body>
        <h1 id="heading">Re ad between the lines</h1>
        <script language="JavaS cript">

        var insultObj = document.create TextNode("Could you *be* any dumber");
        var replyObj = document.create TextNode("Yes, just give me some time");

        heading = document.getEle mentById("headi ng");
        heading.parentN ode.insertBefor e(insultObj, heading);
        heading.parentN ode.insertBefor e(replyObj, null);

        </script>
        </body>
        </html>

        Comment

        • RobG

          #5
          Re: insertBefore (oNewNode [, oChildNode]) problems

          Derek Basch wrote:
          [...][color=blue]
          > insertBefore
          > Inserts the node newChild before the existing child node refChild.
          > If refChild is null, insert newChild at the end of the list of
          > children.[/color]
          [...][color=blue]
          > heading = document.getEle mentById("headi ng");
          > heading.parentN ode.insertBefor e(insultObj, heading);
          > heading.parentN ode.insertBefor e(replyObj, null);[/color]

          Cool. I have been using:

          ...insertBefore (replyObj,lastO ne.nextSibling) ;

          to "insertAfte r" the last child. From now on I'll be using your
          trick - thanks!


          --
          Rob

          Comment

          • Derek Basch

            #6
            Re: insertBefore (oNewNode [, oChildNode]) problems

            Glad I could pay you back for answering my DOM selectors question
            yesterday :). I hope that this discussion can help someone in the
            future as well.

            Comment

            Working...