Solved

Can a node of type TEXT_NODE ever have a sibling?

Posted on 2006-07-14
3
534 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now