Solved

Can a node of type TEXT_NODE ever have a sibling?

Posted on 2006-07-14
3
538 Views
Last Modified: 2006-11-18


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

Thanks!
0
Comment
Question by:0h4crying0utloud
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
dakyd earned 500 total points
ID: 17111584
Yes, they can.  In the example below, the <p> is the parent, there is a text node, and the span is its sibling.  Hope that helps.

<html>
<head>
<script type="text/javascript">
function checkForSib()
{
  var obj = document.getElementById("parent");
  var txtNode = obj.childNodes[0];
  alert("node has type of " + txtNode.nodeType + " and value of '" + txtNode.data + "'");
  var sib = txtNode.nextSibling;
  alert(sib + ": " + sib.innerHTML);
}
</script>
</head>

<body>
<p id="parent">some text <span style="font-weight: bold">sibling of "some text"</span></p>

<form>
  <input type="button" onclick="checkForSib();" value="check for sibling">
</form>
</body>
</html>
0
 
LVL 10

Author Comment

by:0h4crying0utloud
ID: 17111617


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.
0
 
LVL 19

Expert Comment

by:dakyd
ID: 17112798
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>
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question