...

View Full Version : Inserting a node between two existing nodes



Eskimo
01-19-2005, 10:20 PM
If I have:


<form>
<label>form control</label><input id="text0" />
<label>form control</label><input id="text1" />
<label>form control</label><input id="text2" />
</form>


Is there a way to insert a node directly after text0 or text1?

Basscyst
01-19-2005, 10:39 PM
Since the text fields are form elements, I think the most direct way to do it would just be to specify a span after the text box and append your string to it.

Something like:



<html>
<head>
<script type="text/javascript">

function addNode(pstr,i)
{
var str=document.createTextNode(pstr);
var fid="span"+i;
document.getElementById(fid).appendChild(str);
}

</script>
</head>
<body onload="addNode('I like hot dogs!',0)">
<form>
<label>form control</label><input id="text0" /><span id="span0"></span>
<label>form control</label><input id="text1" /><span id="span1"></span>
<label>form control</label><input id="text2" /><span id="span2"></span>
</form>
</body>
</html>


Basscyst

liorean
01-19-2005, 11:55 PM
Well, I think I have the solution for you here: Node (http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247).prototype.insertBefore (http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-952280727)(newChild,refChild)

Eskimo
01-20-2005, 12:19 AM
liorean, you are a gentleman and a scholar.

It works great.

Basscyst
01-20-2005, 12:49 AM
Ahh, I see Liorean. I'm just now trying to break my innerHTML habit so all these nodes and children are still kinda new to me. Sometimes answering a question you might not be totally sure about is the best way to learn something new, actually quite often. ;)

Thanks,
Basscyst



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum