Help! how to extract inner text?

Discussion in 'Javascript' started by kj, Jul 1, 2004.

  1. kj

    kj Guest

    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.
     
    kj, Jul 1, 2004
    #1
    1. Advertising

  2. kj

    Michael Guest

    kj <> wrote in message news:<cc1q1q$nil$>...
    > 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
     
    Michael, Jul 2, 2004
    #2
    1. Advertising

  3. kj

    J Krugman Guest

    In <cc1q1q$nil$> kj <> 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.
     
    J Krugman, Jul 2, 2004
    #3
  4. kj

    Shawn Milo Guest

    kj <> wrote in message news:<cc1q1q$nil$>...
    > 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
     
    Shawn Milo, Jul 3, 2004
    #4
  5. kj

    DU Guest

    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-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
     
    DU, Jul 3, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlo v. Dango
    Replies:
    14
    Views:
    1,087
    Alex Martelli
    Oct 19, 2003
  2. Pyenos
    Replies:
    2
    Views:
    407
    Pyenos
    Dec 27, 2006
  3. Loocek

    extract inner xml...

    Loocek, Mar 17, 2009, in forum: XML
    Replies:
    6
    Views:
    3,922
    anon_anon
    Nov 28, 2009
  4. Golam Md. Shibly
    Replies:
    0
    Views:
    77
    Golam Md. Shibly
    Mar 1, 2014
  5. Jason Friedman
    Replies:
    0
    Views:
    92
    Jason Friedman
    Mar 8, 2014
Loading...

Share This Page