473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.getElementById("row1").parentNode.appendC hild(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.

Dec 18 '06 #1
18 1301
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

Dec 18 '06 #2
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.getElementById("row1").parentNode.appendC hild(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

Dec 18 '06 #3
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

Dec 18 '06 #4
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)
Dec 18 '06 #5
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)
Dec 19 '06 #6
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/
Dec 19 '06 #7

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-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
HTMLTableRowElement (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

Dec 19 '06 #8
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.
Dec 19 '06 #9
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.

Dec 19 '06 #10
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-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
HTMLTableRowElement (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

Dec 19 '06 #11
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.

Dec 19 '06 #12
>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.

Dec 20 '06 #13
In comp.lang.javascript message
<11**********************@73g2000cwn.googlegroups. com>, Mon, 18 Dec 2006
21:17:58, Brandon <bc******@creighton.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.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 20 '06 #14
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. ;-)

Dec 21 '06 #15
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)
Dec 21 '06 #16
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.

Dec 21 '06 #17
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)
Dec 22 '06 #18
In comp.lang.javascript message
<11**********************@48g2000cwx.googlegroups. com>, Thu, 21 Dec 2006
15:48:11, Brandon <bc******@creighton.eduwrote:
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.
Dec 23 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Miguel J. Jiménez | last post by:
Hi, I have the following node: <node> Some text here with lots of inside it... </node> and I would like it to transfrom it using XSLT to the following: Some text<br/> here with</br> lots...
1
by: Luke Dalessandro | last post by:
I have an application where there is a primary XML data file. I'll use the following as an example: <data> <item id="a"> <name>A</name> <price>$10</price> </item> <item id="b">...
4
by: Lukasz Indyk | last post by:
hello;) i have a piece of html code, and want to replace every image on my page with this code. now i do it by replacing IMG node with SPAN node, and then setting innerHTML property of SPAN node...
3
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my...
3
XtinaS
by: XtinaS | last post by:
I'm trying to write a script for Greasemonkey that will, in LiveJournal, replace a placeholdered embedded YouTube thing with a link to the video. In LiveJournal, you can set an option to have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.