...

View Full Version : insert a new Node



Kor
10-15-2004, 11:57 AM
is there a quick mode to insert a new Node inside a Node (the new one will become a Node for all the Childs)?

Ex I have

<td>
--childs here--
</td>

I need
<td>
<div>
--childs here--
</div>
</td>

liorean
10-15-2004, 02:39 PM
Well, nothing shorter than
var
parent=td.appendChild(document.createElement('div'));if that's what you mean.

Kor
10-15-2004, 03:13 PM
Nope, I tried, but the result is:

I need
<td>
--childs here--
<div>
</div>
</td>

I need to nest all the parent's childs inside the new created element.

I tried remove the childs, append the cteated element and append inside the removed childs but it wount work either. On one hand, the removeChild leaves unexpectable tags inside the parent (such as <br>) and in fact the variable is an array of objects and I am not sure how to concatenate array's elements if they are objects...

liorean
10-15-2004, 06:17 PM
Well, this sounds like a simple fix, but I need to see some code for it.

hemebond
10-15-2004, 11:23 PM
Internet Explorer gets this wrong even though they claim to support everything here:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>46092</title>
<style type="text/css">
p {
border:1px solid #f00;
}
div {
border:1px solid #0f0;
padding:1em;
}
</style>
</head>
<body>
<div id="div">
<p>
Text
</p>
</div>
</body>
<script type="text/javascript">
var div = document.getElementById('div');
var new_div = document.createElement('div');

for(var i = 0; i < div.childNodes.length; i++)
{
new_div.appendChild(div.childNodes[i].cloneNode(true));
}
for(i = div.childNodes.length - 1; i > 0; i--)
{
div.removeChild(div.childNodes[i]);
}
div.appendChild(new_div);
</script>
</html>

codegoboom
10-16-2004, 07:05 AM
for(i = div.childNodes.length - 1; i > 0; i--)
{
div.removeChild(div.childNodes[i]);
}


try this: ;)



for(i = div.childNodes.length - 1; i >= 0; i--)

hemebond
10-16-2004, 07:09 AM
Ah of course. Hmm. Why the heck did it work in Firefox?

Ah got it. Gecko includes text nodes as child nodes. This means I was leaving in the very first child node, which in Firefox was just a text node of a tab. Internet Explorer ignores text nodes when referencing them through childNodes.

Kor
10-17-2004, 12:17 PM
Internet Explorer ignores text nodes when referencing them through childNodes


Not exactly. IE ignore text nodes as childNodes when their NodeValue is null.

hemebond
10-17-2004, 11:51 PM
But a tab character isn't null. Stupid IE.

Kor
10-18-2004, 09:32 AM
I wonder if it is more stupid than considering, as Moz does, the HTML "Tab space" as a valueable child

free space(non-space)
<td>
</td>
Is evaluated by Moz as a firstChild, but
<td></td>
it is not

Seems an equivalent stupidity for me as well ...

Probably when we all will start using <tag /> notation problem would be easier to handle.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum