473,670 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I get an element's the text node

I want if possible to use something like:

element.getElem entsByTagName(' text')

but that doesn't work. Is there another value for the parameter, or is it
not possible with getElementsByTa gName?

Thanks,
Ron

Jul 23 '05 #1
6 28414
Ron Brennan wrote:
I want if possible to use something like:

element.getElem entsByTagName(' text')

but that doesn't work. Is there another value for the parameter, or is it
not possible with getElementsByTa gName?


As far as I know, no, unless you have a tag named <text>, in which case
"element.getEle mentsByTagName( 'text')" returns a collection of <text>
elements contained in "element"

Mick

Jul 23 '05 #2
Ron Brennan wrote:
I want if possible to use something like:

element.getElem entsByTagName(' text')

but that doesn't work. Is there another value for the parameter, or is it
not possible with getElementsByTa gName?


As Mick said you cannot do it with getElementsByTa gName, this method
returns *elements*, i.e. nodes of type "element", and not other types of
nodes (at least it shouldn't).

If you want to retrieve only text nodes, then you'll have to build
custom node iterators; depending on your requirements this may not be
too difficult.

FYI the W3C has defined traversal objects, namely Treewalkers or
NodeIterators, which permit to traverse the tree in a certain order,
with node filters, and much more. Unfortunately these are not supported
in IE (and probably lots of other browsers), which makes the objects
unusable for the web.

<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/traversal.html>

Try the following in Opera 8 or Mozilla 1.7+ to get an illustration.

---
<div id="foo">Hello, <span>World</span>!</div>

<script type="text/javascript">
if(
document.getEle mentById &&
document.create TreeWalker &&
typeof NodeFilter!="un defined"
){

var tw=document.cre ateTreeWalker(
document.getEle mentById("foo") ,
NodeFilter.SHOW _TEXT,
null,
false
);
var node;
var buf=[];

while(node=tw.n extNode())
buf.push(node.n odeValue);

alert(buf.lengt h ? buf.join("\n") : "No text node iterated");
}
</script>
---
HTH,
Yep.
Jul 23 '05 #3
Yup, it appears impossible that way. The following is an acceptable
solution:

var text = getElementById( "tdElement").fi rstChild.data;

Jul 23 '05 #4
VK
> The following is an acceptable solution:
var text = getElementById( "tdElement").fi *rstChild.data;


If you're working with your own pages where you're the king.

Universally *in HTML* there is no guarantee that the firstChild will
always point to the text node (even if you see nothing but text in the
element of question). It may return an empty text node created say on
place of the page break.

Jul 23 '05 #5

If you're looking for the text of the node with no html and all
concatenated in one, so called NORMALIZED()'ed node, for mozilla
obj.textContent gives you all that, stripped of all html and any null
chars like tabs and CR's, for IE/opera you can use obj.innerText, is
exactly the same object. Now, if you want to do it per node over the
childNodes collection, then you can iterate of the object you want to
check, check if its obj.childNodes. length is greater than 0, or has
children, then check each of its children for obj.childNodes. length as
well, for any children in them. Anyhow, in the loop, check each
obj.nodeType, #text nodes have a DOM nodeType of 3, elements are 1,
attributes are 2 and so on, check your DOM glossary if you wish.
Danny

On Sat, 02 Jul 2005 10:55:08 -0700, Ron Brennan <rb******@magma .ca> wrote:
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #6
VK wrote:
The following is an acceptable solution:
var text = getElementById( "tdElement").fi *rstChild.data;
If you're working with your own pages where you're the king.


No, because it has to be `document.getEl ementById'.
Universally *in HTML* there is no guarantee that the firstChild will
always point to the text node (even if you see nothing but text in the
element of question).
Correct, the text may be marked up by another element.
It may return an empty text node created
There is no such thing as an empty text node in an unchanged DOM tree;
either the text node contains whitespace or the (first child node) node
is an element node.
say on place of the page break.


What is a page break in HTML?
PointedEars
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
17807
by: Juliano Freitas | last post by:
How can i get the text between the <teste> tags?? >>> xml = """<root><teste> texto </teste></root>""" >>> from xml.dom import minidom >>> document = minidom.parseString(xml) >>> document <xml.dom.minidom.Document instance at 0x4181df0c> >>> minidom.getElementsByTagName('teste') >>> element = document.getElementsByTagName('teste')
2
3128
by: Martin | last post by:
Hallo, can you help me writing a generic xslt transformation (useable with xsql from oracle)? The problem is how to get the escaping characters .... === INPUT-File in.xml <?xml version = '1.0'?> <person><who>scott</who></person>
5
3114
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are the defined contents of the TABLE element. The TR element is not defined as an "immediate" or "direct" contained element of TABLE. Given that
6
13355
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... html file (generated using .NET 2.0 beta2): <form method="post" action="Test2.aspx" id="form1">
3
5501
by: Robert Oschler | last post by:
What's a good way to find a specific text node element in a web page's DOM tree? I thought of traversing each node but there has to be a faster way. Is there a "find text node by nodeValue" function or something? I do know the nodeValue (text string) of the text node. Thanks.
1
1747
by: jason.lucey | last post by:
Hi, I cannot get this figured out. I need to get the text out of an element node. In IE, I can do it like this: xNode(0).text but that does not work for Mozilla. And since element nodes return NULL for nodeValue, what am I supposed to do? is there a way to convert an element node into a text node?
8
2280
by: bennett.matthew | last post by:
Hello all, This is probably an elementary (no pun intended) question, but I've spent all afternoon on it and it's driving me crazy. I have a function which dynamically adds to a table. It receives a variable which basically encapsulates this: <div id="tableid"> <tr>
3
2188
markmcgookin
by: markmcgookin | last post by:
Hi, I have the following XML <AnswerList xmlns="http://tempuri.org/ALPS_Assessmentv1p1_RESCO_Schema.xsd"> <DateTimeLastSaved>12:12:12 1900</DateTimeLastSaved> <UserName>Bob</UserName> <AssessmentName>Name of Assessment</AssessmentName> <AssessmentID>AssID</AssessmentID>
0
8468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7415
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4209
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1792
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.