Solved

Can a node of type TEXT_NODE ever have a sibling?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

920 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