Link to home
Start Free TrialLog in
Avatar of 0h4crying0utloud
0h4crying0utloud

asked on

Can a node of type TEXT_NODE ever have a sibling?



Can a node of type TEXT_NODE ever have a sibling? If so please provide an example.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of dakyd
dakyd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 0h4crying0utloud
0h4crying0utloud

ASKER



Doh!  what I meant was can a textNode ever have another textNode sibling. But you anwser is correct to this question, I'll post another.
Ah, I misunderstood then.  I did some testing, and in a normal HTML document, browsers are smart enough to combine the two would-be text nodes.  An interesting side note is that in the Mozilla browsers, a new line between two html tags causes a textnode to be inserted.  So this:

  <tr>
    <td>blah ...

   Would mean that the first child of the <tr> would be a text node with just "\n", and the second child would be the <td>.  It has to do with the implementation of the browser, but that construct is fairly common, so you often get "extra" text nodes all over the DOM if you're using that browser.

That aside, it's still technically possible to do what you're asking.  It's a bit contrived, but you can use the DOM to forcibly insert a textnode anywhere you want, including as the sibling of another text node:

<html>
<head>
<script type="text/javascript">
function addTxtNode()
{
  var obj = document.getElementById("parent");
  var txt = document.createTextNode(" world");
  obj.appendChild(txt);

  alert("text node1: '" + obj.childNodes[0].data + "'\n" +
        "text node2: '" + obj.childNodes[1].data + "'");
}
</script>
</head>

<body>
<p id="parent">hello<p>

<form>
  <input type="button" value="make text node siblings" onclick="addTxtNode();">
</form>
</body>
</html>