Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Help! how to extract inner text?

Reply
Thread Tools

Help! how to extract inner text?

 
 
kj
Guest
Posts: n/a
 
      07-01-2004



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>.

--
NOTE: In my address everything before the period is backwards.
 
Reply With Quote
 
 
 
 
Michael
Guest
Posts: n/a
 
      07-02-2004
kj <(E-Mail Removed)> wrote in message news:<cc1q1q$nil$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
 
 
 
J Krugman
Guest
Posts: n/a
 
      07-02-2004
In <cc1q1q$nil$(E-Mail Removed)> kj <(E-Mail Removed)> writes:


>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?


For IE (at least older versions) try the property innerText on the
TD element.

jill

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
Shawn Milo
Guest
Posts: n/a
 
      07-02-2004
kj <(E-Mail Removed)> wrote in message news:<cc1q1q$nil$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
DU
Guest
Posts: n/a
 
      07-03-2004
kj wrote:

> 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-Le...e3-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
extract inner xml... Loocek XML 6 11-28-2009 09:30 PM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments