Replace one DOM node with two?

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

    Replace one DOM node with two?

    I have a question about manipulating a document. Suppose for example
    that I had a table like this:

    <table>
    <tr id="row1">
    <td>R1C1</td>
    </tr>
    <tr>
    <td>R2C1</td>
    </tr>
    </table>

    What I want to do is add an onclick event handler to row1 to insert a
    row after row1. I can't seem to find a way to do it though. After
    creating the new row node, I could try something like
    document.getEle mentById("row1" ).parentNode.ap pendChild(newNo de) but
    that would add the new row to the bottom of the table. The
    insertBefore() method is the right idea but I want to insert the new
    row AFTER row1 and there doesn't seem to be an insertAfter() method.

    I thought about navigating through the DOM tree to get the row after
    row1 and then using insertBefore() but the table is generated
    dynamically and there won't necessarily be a next row. Any ideas?

    -Brandon R.

  • Brandon

    #2
    Re: Replace one DOM node with two?

    Oh, and I suppose I probably should have mentioned that I would also
    settle for replacing one node with two if that is possible. I know
    there is a replaceNode() method but is there a way to replace one <tr>
    nodes with two <trnodes? It would be sort of a hack but I would
    settle for it.

    -Brandon

    Comment

    • Peter Michaux

      #3
      Re: Replace one DOM node with two?

      Brandon wrote:
      I have a question about manipulating a document. Suppose for example
      that I had a table like this:
      >
      <table>
      <tr id="row1">
      <td>R1C1</td>
      </tr>
      <tr>
      <td>R2C1</td>
      </tr>
      </table>
      If you want to manipulate rows in a table then it is a good idea to
      include the <tbodytags for IE.

      <table>
      <tbody>
      <tr id="row1">
      <td>R1C1</td>
      </tr>
      <tr>
      <td>R2C1</td>
      </tr>
      </tbody>
      </table>

      What I want to do is add an onclick event handler to row1 to insert a
      row after row1. I can't seem to find a way to do it though. After
      creating the new row node, I could try something like
      document.getEle mentById("row1" ).parentNode.ap pendChild(newNo de) but
      that would add the new row to the bottom of the table. The
      insertBefore() method is the right idea but I want to insert the new
      row AFTER row1 and there doesn't seem to be an insertAfter() method.
      var r1 = document.getEle mentById("row1" );
      r1.parentNode.i nsertBefore(new Node, r1.nextSibling) ;

      If r1 is the last child in the parentNode then nextSibling is null and
      the newNode will be inserted at the end of the list.

      Peter

      Comment

      • Brandon

        #4
        Re: Replace one DOM node with two?

        That did the trick. Thanks Peter.

        By the way, to anyone else who might be in the same situation, it turns
        out that tables have insertRow(index ) methods and rows have
        insertCell(inde x) methods. That works for tables but only tables so if
        you are dealing with div tags (like I actually will be eventually,)
        using insertBefore() with a node's nextSibling appears to be the way to
        go.

        -Brandon

        Comment

        • Evertjan.

          #5
          Re: Replace one DOM node with two?

          Brandon wrote on 18 dec 2006 in comp.lang.javas cript:
          That did the trick. Thanks Peter.
          What thrick?
          By the way, to anyone else who might be in the same situation, it turns
          What situation?
          out that tables have insertRow(index ) methods and rows have
          insertCell(inde x) methods. That works for tables but only tables so if
          you are dealing with div tags (like I actually will be eventually,)
          using insertBefore() with a node's nextSibling appears to be the way to
          go.

          Please always quote on usenet.

          Keep to usenet netiquette. This is not email.
          Others using different news servers could not have the whole thread
          available.

          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress)

          Comment

          • Brandon

            #6
            Re: Replace one DOM node with two?

            Aside from a hypothetical "what if," what is a practical situation
            where the rest of the thread wouldn't be available to a user? The idea
            of threads is such a fundamental concept for usenet that I'm not too
            concerned about quoting when there was only one response and I named
            the author in my response... especially when anyone could look up the
            rest of the thread on Google if needed.

            Please, no more netiquette posts, especially if you are going to
            "correct" me about responding before the quote block. ;-)
            That did the trick. Thanks Peter.
            >
            What thrick?
            >
            By the way, to anyone else who might be in the same situation, it turns
            >
            What situation?
            >
            out that tables have insertRow(index ) methods and rows have
            insertCell(inde x) methods. That works for tables but only tables so if
            you are dealing with div tags (like I actually will be eventually,)
            using insertBefore() with a node's nextSibling appears to be the way to
            go.
            >
            >
            Please always quote on usenet.
            >
            Keep to usenet netiquette. This is not email.
            Others using different news servers could not have the whole thread
            available.
            >
            --
            Evertjan.
            The Netherlands.
            (Please change the x'es to dots in my emailaddress)

            Comment

            • Randy Webb

              #7
              Re: Replace one DOM node with two?

              Brandon said the following on 12/19/2006 12:17 AM:
              Aside from a hypothetical "what if," what is a practical situation
              where the rest of the thread wouldn't be available to a user?
              That question can't be answered within the constraints you have placed
              on it. Personally, I didn't access to the rest of the thread you were
              replying to until I went through the trouble of digging it out. Had you
              bothered to quote what you were replying to, it wouldn't have been an issue.

              Besides, if you quote, there is *never* a doubt as to what you are
              replying to - that can't be said for not quoting at all.

              --
              Randy
              Chance Favors The Prepared Mind
              comp.lang.javas cript FAQ - http://jibbering.com/faq
              Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

              Comment

              • RobG

                #8
                Re: Replace one DOM node with two?


                Brandon wrote:
                By the way, to anyone else who might be in the same situation, it turns
                out that tables have insertRow(index ) methods and rows have
                insertCell(inde x) methods. That works for tables but only tables
                For user agents implementing the W3C DOM 2 HTML Specification[1], the
                insertRow method is available for interfaces HTMLTableElemen t (table
                elements) and HTMLTableSectio nElement (thead, tfoot and tbody
                elements).

                <URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903 >
                <URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-93995626 >


                The insertCell method is only available for interface
                HTMLTableRowEle ment (tr elements).

                <URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016 >


                1. Which includes most browsers, but to different levels of conformity.


                --
                Rob

                Comment

                • Richard Cornford

                  #9
                  Re: Replace one DOM node with two?

                  Brandon wrote:
                  Aside from a hypothetical "what if," what is a practical
                  situation where the rest of the thread wouldn't be available
                  to a user?
                  Whenever the (or any) older messages in a thread are no longer available
                  on the user's news server (with a group as active as c.l.js that can be
                  anything older than a couple of days in extreme cases).
                  The idea of threads is such a fundamental concept for
                  usenet that I'm not too concerned about quoting when
                  there was only one response and I named the author
                  in my response...
                  Your personal concerns are of no relevance to what may represent the
                  optimum posting pattern.
                  especially when anyone could look
                  up the rest of the thread on Google if needed.
                  Such efforts should not be needed.
                  Please, no more netiquette posts, especially if you are
                  going to "correct" me about responding before the quote
                  block. ;-)
                  <snip>

                  Even if you are too self-obsessed to consider others it still remains
                  necessary to publicly deprecate top-posting as your encouraging others
                  to imitate you (even by example) will diminish their chances of
                  receiving useful help here, and so do harm if not corrected.

                  Richard.


                  Comment

                  • Brandon

                    #10
                    Re: Replace one DOM node with two?

                    The idea of threads is such a fundamental concept for
                    usenet that I'm not too concerned about quoting when
                    there was only one response and I named the author
                    in my response...
                    >
                    Your personal concerns are of no relevance to what may represent the
                    optimum posting pattern.
                    The VAST majority of my posts DO quote the original post and DO have
                    the reply after what I was responding to. Although it seems pretty
                    obvious that the over zealous Usenet Police are the "shoot first, ask
                    questions later" type when it comes to the "optimum posting pattern."
                    You can't bitch at every last post that isn't arranged the way you
                    would have arranged it and expect a warm response.

                    Comment

                    • Brandon

                      #11
                      Re: Replace one DOM node with two?

                      By the way, to anyone else who might be in the same situation, it turns
                      out that tables have insertRow(index ) methods and rows have
                      insertCell(inde x) methods. That works for tables but only tables
                      >
                      For user agents implementing the W3C DOM 2 HTML Specification[1], the
                      insertRow method is available for interfaces HTMLTableElemen t (table
                      elements) and HTMLTableSectio nElement (thead, tfoot and tbody
                      elements).
                      <URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903 >
                      <URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-93995626 >
                      The insertCell method is only available for interface
                      HTMLTableRowEle ment (tr elements).
                      <URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016 >
                      1. Which includes most browsers, but to different levels of conformity.
                      --
                      Rob
                      That is correct. Thanks for the response and links Rob.

                      -Brandon

                      Comment

                      • Richard Cornford

                        #12
                        Re: Replace one DOM node with two?

                        Brandon wrote:
                        >>The idea of threads is such a fundamental concept for
                        >>usenet that I'm not too concerned about quoting when
                        >>there was only one response and I named the author
                        >>in my response...
                        >>
                        >Your personal concerns are of no relevance to what may represent the
                        >optimum posting pattern.
                        >
                        The VAST majority of my posts DO quote the original post and
                        DO have the reply after what I was responding to.
                        I imagine that they also do not suggest that top-posting should be
                        acceptable.
                        Although it seems pretty obvious that the over zealous Usenet
                        Police are the "shoot first, ask questions later" type when it comes
                        to the "optimum posting pattern."
                        Whether a post is top-posted or not is not a subjective judgment, so
                        what questions would you have asked?
                        You can't bitch at every last post that isn't arranged the way you
                        would have arranged it and expect a warm response.
                        Why do you think I would care how warmly you receive and/or respond to
                        your criticism?

                        Richard.

                        Comment

                        • Brandon

                          #13
                          Re: Replace one DOM node with two?

                          >The idea of threads is such a fundamental concept for
                          >usenet that I'm not too concerned about quoting when
                          >there was only one response and I named the author
                          >in my response...
                          >
                          Your personal concerns are of no relevance to what may represent the
                          optimum posting pattern.
                          The VAST majority of my posts DO quote the original post and
                          DO have the reply after what I was responding to.
                          >
                          I imagine that they also do not suggest that top-posting should be
                          acceptable.
                          Depends - my long replies with lots of individual responses to specific
                          parts of the original post are pretty much always bottom-posted (like
                          this one.) Some others are not. Read on...
                          Although it seems pretty obvious that the over zealous Usenet
                          Police are the "shoot first, ask questions later" type when it comes
                          to the "optimum posting pattern."
                          >
                          Whether a post is top-posted or not is not a subjective judgment, so
                          what questions would you have asked?
                          Whether it is top-posted is not subjective, like you said. But whether
                          or not it is acceptable IS subjective. How about asking this question:
                          Is the response "chatty" (lots of little back-and-forth responses) or
                          is it a "one piece" response with a fair amount of new content? If it
                          is the second, then posting at the top might not be the atrocity you
                          are making it out to be. It's neither uncommon nor inappropriate on
                          usenet (although only in certain situations) and in the corporate
                          world, it's pretty much the standard.

                          I'm sure you will disagree with some sort of "but then someone
                          somewhere would have to scroll down and that would be an inconvenience"
                          argument but all in all, there are no formal rules, you aren't the
                          usenet police, and top-posting makes sense some times.
                          You can't bitch at every last post that isn't arranged the way you
                          would have arranged it and expect a warm response.
                          >
                          Why do you think I would care how warmly you receive and/or respond to
                          your criticism?
                          Well, you kids seem awfully concerned about the whole matter.

                          Comment

                          • Dr J R Stockton

                            #14
                            Re: Replace one DOM node with two?

                            In comp.lang.javas cript message
                            <1166505478.327 701.317050@73g2 000cwn.googlegr oups.com>, Mon, 18 Dec 2006
                            21:17:58, Brandon <bcr07548@creig hton.eduwrote:
                            >
                            >Please, no more netiquette posts, especially if you are going to
                            >"correct" me about responding before the quote block. ;-)

                            Those who disregard requests to honour the accepted Usenet conventions
                            as described in the newsgroup FAQ and in FYI28/RFC1855 are liable to
                            find their own requests for assistance ignored by the regular experts
                            here, though they may still get responses from the less capable.

                            It's a good idea to read the newsgroup and its FAQ. See below.

                            --
                            (c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
                            news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
                            <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
                            <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

                            Comment

                            • Brandon

                              #15
                              Re: Replace one DOM node with two?

                              Those who disregard requests to honour the accepted Usenet conventions
                              as described in the newsgroup FAQ and in FYI28/RFC1855 are liable to
                              find their own requests for assistance ignored by the regular experts
                              here, though they may still get responses from the less capable.
                              >
                              It's a good idea to read the newsgroup and its FAQ. See below.
                              I still insist that a lot of the semi-trivial stuff you three are so
                              overly concerned about is neither uncommon NOR INAPPROPRIATE in the
                              right situation. Not all situations (I'll give you that much,) but
                              some. Read my last post for more information.

                              I don't mean any disrespect but it pisses me off when the self-appoint
                              usenet police get preachy over A SINGLE POST. If all the "experts" are
                              going to do is knit-pick over people's posting habits, maybe I don't
                              want the help of those particular experts. ;-)

                              Comment

                              Working...