Replace one DOM node with two?

B

Brandon

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.
 
B

Brandon

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
 
P

Peter Michaux

Brandon said:
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>
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
 
B

Brandon

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
 
E

Evertjan.

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.
 
B

Brandon

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

Randy Webb

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.
 
R

RobG

Brandon said:
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.
 
R

Richard Cornford

Brandon said:
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.
 
B

Brandon

The idea of threads is such a fundamental concept for
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.
 
B

Brandon

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.

That is correct. Thanks for the response and links Rob.

-Brandon
 
R

Richard Cornford

Brandon said:
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.
 
B

Brandon

The idea of threads is such a fundamental concept for
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...
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.
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.
 
D

Dr J R Stockton

In comp.lang.javascript message
Mon said:
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.
 
B

Brandon

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

Evertjan.

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?
 
E

Evertjan.

Brandon wrote on 22 dec 2006 in comp.lang.javascript:
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.


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.
 
D

Dr J R Stockton

In comp.lang.javascript message
Thu said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top