Help! how to extract inner text?

K

kj

I have elements of the form

<TD><A name="marker"></A>foobar</TD>

and I want to extract the text "foobar". How can I do this in IE?

(I think I know how to do it for NS). Is there a way to implement
this that is reasonably browser-independent?

Many thanks!

kj

P.S. Originally, the elements were of the form

<TD><A name="marker"/>foobar</TD>

but Mozilla would silently convert them to

<TD><A name="marker">foobar</A></TD>

so I've reverted to the format with the empty <A ...></A>.
 
M

Michael

kj said:
I have elements of the form

<TD><A name="marker"></A>foobar</TD>

and I want to extract the text "foobar". How can I do this in IE?

(I think I know how to do it for NS). Is there a way to implement
this that is reasonably browser-independent?

Many thanks!

kj

P.S. Originally, the elements were of the form

<TD><A name="marker"/>foobar</TD>

but Mozilla would silently convert them to

<TD><A name="marker">foobar</A></TD>

so I've reverted to the format with the empty <A ...></A>.


Hi,

In NS 6.2, Mozilla 1.7 and IE 6 the following is working fine:

<TD><A name="marker">foobar</A></TD>

document.getElementsByName('marker')[0].firstChild.nodeValue
alternative
document.getElementsByName('marker')[0].innerHTML

cu, Michael
 
S

Shawn Milo

kj said:
I have elements of the form

<TD><A name="marker"></A>foobar</TD>

and I want to extract the text "foobar". How can I do this in IE?

(I think I know how to do it for NS). Is there a way to implement
this that is reasonably browser-independent?

Many thanks!

kj

P.S. Originally, the elements were of the form

<TD><A name="marker"/>foobar</TD>

but Mozilla would silently convert them to

<TD><A name="marker">foobar</A></TD>

so I've reverted to the format with the empty <A ...></A>.



Try a regular expression:

<script type="text/javascript">
var someHTML = '<TD><A name="marker"></A>foobar</TD>';
var extracted = '';
alert(someHTML);
extracted = someHTML.replace(/^.*a>([^<]+).*$/i, '$1');
alert(extracted);
</script>


Shawn
 
D

DU

kj said:
I have elements of the form

<TD><A name="marker"></A>foobar</TD>

and I want to extract the text "foobar". How can I do this in IE?

(I think I know how to do it for NS). Is there a way to implement
this that is reasonably browser-independent?

Many thanks!

kj

P.S. Originally, the elements were of the form

<TD><A name="marker"/>foobar</TD>

but Mozilla would silently convert them to

<TD><A name="marker">foobar</A></TD>

so I've reverted to the format with the empty <A ...></A>.

Use entirely valid DOM method for this as much as possible: you make
your code more accessible, robust, future-proof.

Assuming this is your html code:

<td><a name="marker">foobar</a></td>

then

function ExtractInnerText(NodeObject)
{
if(NodeObject.textContent)
/*
Mozilla-based browsers (1.5+) support DOM 3 attribute textContent:
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent
*/
{
return NodeObject.textContent;
}
else if(NodeObject.childNodes[0] && NodeObject.childNodes[0].nodeType ==
"TEXT_NODE")
/*
DOM 2 Core attributes supported by MSIE 5.x, MSIE 6, Mozilla-based
browsers, Opera 7.x, K-meleon 0.8+, Safari 1.x, Konqueror 3.x, Galeon
1.x, etc.
*/
{
return NodeObject.childNodes[0].nodeValue;
}
else if(NodeObject.innerText)
/* MSIE 4 and other old or deprecated or odd ball browsers */
{
return NodeObject.innerText;
};
}

More explanations, tutorial, examples given at:

Nodes, Walking through the DOM tree, Getting an element, Changing a node
http://www.quirksmode.org/dom/intro.html#link2

Using the W3C DOM Level 1 Core
http://www.mozilla.org/docs/dom/technote/intro/

DU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top