Replace one DOM node with two?

Discussion in 'Javascript' started by Brandon, Dec 18, 2006.

  1. Brandon

    Brandon Guest

    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.getElementById("row1").parentNode.appendChild(newNode) 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, Dec 18, 2006
    #1
    1. Advertising

  2. Brandon

    Brandon Guest

    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 <tr> nodes? It would be sort of a hack but I would
    settle for it.

    -Brandon
    Brandon, Dec 18, 2006
    #2
    1. Advertising

  3. 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 <tbody> tags 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.getElementById("row1").parentNode.appendChild(newNode) 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.getElementById("row1");
    r1.parentNode.insertBefore(newNode, 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
    Peter Michaux, Dec 18, 2006
    #3
  4. Brandon

    Brandon Guest

    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(index) 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
    Brandon, Dec 18, 2006
    #4
  5. Brandon

    Evertjan. Guest

    Brandon wrote on 18 dec 2006 in comp.lang.javascript:

    > 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(index) 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)
    Evertjan., Dec 18, 2006
    #5
  6. Brandon

    Brandon Guest

    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(index) 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)
    Brandon, Dec 19, 2006
    #6
  7. Brandon

    Randy Webb Guest

    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.javascript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Dec 19, 2006
    #7
  8. Brandon

    RobG Guest

    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(index) 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 HTMLTableElement (table
    elements) and HTMLTableSectionElement (thead, tfoot and tbody
    elements).

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


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

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


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


    --
    Rob
    RobG, Dec 19, 2006
    #8
  9. 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.
    Richard Cornford, Dec 19, 2006
    #9
  10. Brandon

    Brandon Guest

    > > 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.
    Brandon, Dec 19, 2006
    #10
  11. Brandon

    Brandon Guest

    > > 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(index) 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 HTMLTableElement (table
    > elements) and HTMLTableSectionElement (thead, tfoot and tbody
    > elements).
    > <URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903 >
    > <URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-93995626 >
    > The insertCell method is only available for interface
    > HTMLTableRowElement (tr elements).
    > <URL: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#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
    Brandon, Dec 19, 2006
    #11
  12. 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.
    Richard Cornford, Dec 19, 2006
    #12
  13. Brandon

    Brandon Guest

    > >>> 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.
    Brandon, Dec 20, 2006
    #13
  14. In comp.lang.javascript message
    <>, Mon, 18 Dec 2006
    21:17:58, Brandon <> wrote:
    >
    >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.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, Dec 20, 2006
    #14
  15. Brandon

    Brandon Guest

    > 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. ;-)
    Brandon, Dec 21, 2006
    #15
  16. Brandon

    Evertjan. Guest

    Brandon wrote on 21 dec 2006 in comp.lang.javascript:

    > 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. ;-)


    The experts will be dismayed, their live is focussed on advising you.

    On usenet however one specifies the posting, not the reply.

    > ... over A SINGLE POST ...


    So you even expect those experts to keep track of your postings,
    and preach at posting number ten?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Dec 21, 2006
    #16
  17. Brandon

    Brandon Guest

    > > 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. ;-)

    >
    > The experts will be dismayed...


    I'm sure. You're bordering on troll-level pickiness.
    Brandon, Dec 21, 2006
    #17
  18. Brandon

    Evertjan. Guest

    Brandon wrote on 22 dec 2006 in comp.lang.javascript:

    >> > 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.
    >> > ;-)

    >>
    >> The experts will be dismayed...

    >
    > I'm sure. You're bordering on troll-level pickiness.


    If you are so sure, please explain why you think these experts are just
    there for you, as usenet is not a payed helpdesk.

    You conviently forgot my other lines [inserted here:]

    > On usenet however one specifies the posting, not the reply.
    >
    >> ... over A SINGLE POST ...

    >
    > So you even expect those experts to keep track of your postings,
    > and preach at posting number ten?


    Please reply on wether you also agree with these, dear friend.



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Dec 22, 2006
    #18
  19. In comp.lang.javascript message
    <>, Thu, 21 Dec 2006
    15:48:11, Brandon <> wrote:
    >> > 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. ;-)

    >>
    >> The experts will be dismayed...

    >
    >I'm sure. You're bordering on troll-level pickiness.


    Please give a proper attribution line, so that those with well-designed
    software can readily see who you are responding to.

    See signature line 2 for full guidance.

    --
    (c) John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 6.05.
    Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
    Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
    No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
    Dr J R Stockton, Dec 23, 2006
    #19
    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. Replies:
    0
    Views:
    1,414
  2. Replies:
    0
    Views:
    365
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,406
    Dimitre Novatchev
    Aug 24, 2006
  4. Peter Mueller
    Replies:
    6
    Views:
    4,533
    Stefan Ram
    Jan 13, 2008
  5. Lukasz Indyk
    Replies:
    4
    Views:
    123
    Lukasz Indyk
    Apr 6, 2004
Loading...

Share This Page