Why won't this work

Discussion in 'Javascript' started by sjoshi, Apr 3, 2007.

  1. sjoshi

    sjoshi Guest

    I have this HTML fragment

    The <abbr title="World Wide Web Consortium">W3C</abbr>

    When I try to get the textNode's value, I keep getting an error saying
    "object required". On debugging it fails when trying to fet the
    lastChild.nodeValue

    //This works, I get an element node
    var currentAbbr = abbrs;
    //This also works and sys World Wide Web Consortium"
    var abbrTitle = currentAbbr.getAttribute("title");
    //*** This fails ***
    var abbrText = currentAbbr.lastChild.nodeValue;

    Any hints are greatly appreciated.

    thanks
    Sunit
    sjoshi, Apr 3, 2007
    #1
    1. Advertising

  2. sjoshi

    Evertjan. Guest

    sjoshi wrote on 03 apr 2007 in comp.lang.javascript:

    > I have this HTML fragment
    >
    > The <abbr title="World Wide Web Consortium">W3C</abbr>
    >
    > When I try to get the textNode's value, I keep getting an error saying
    > "object required". On debugging it fails when trying to fet the
    > lastChild.nodeValue


    Always state the browser and the error text, please.

    > //This works, I get an element node
    > var currentAbbr = abbrs;


    I doubt that, not under IE7.

    Use:

    var currentAbbr = document.getElementsByTagName('abbr');


    > //This also works and sys World Wide Web Consortium"
    > var abbrTitle = currentAbbr.getAttribute("title");


    var abbrTitle = currentAbbr.title;

    works just as nice.

    > //*** This fails ***
    > var abbrText = currentAbbr.lastChild.nodeValue;


    works fine here [IE7]

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Apr 3, 2007
    #2
    1. Advertising

  3. sjoshi

    RobG Guest

    On Apr 3, 8:11 pm, "Evertjan." <> wrote:
    > sjoshi wrote on 03 apr 2007 in comp.lang.javascript:
    >
    > > I have this HTML fragment

    >
    > > The <abbr title="World Wide Web Consortium">W3C</abbr>

    >
    > > When I try to get the textNode's value, I keep getting an error saying
    > > "object required". On debugging it fails when trying to fet the
    > > lastChild.nodeValue

    [...]
    > > //*** This fails ***
    > > var abbrText = currentAbbr.lastChild.nodeValue;

    >
    > works fine here [IE7]


    But not in IE 6, it seems to believe that the abbr element doesn't
    have a child node.

    The OP should really be using textContent or innerText as the text
    within the abbr could easily be styled by some other in-line element,
    e.g.:

    <abbr onclick="alert(getText(this));"
    style="font-weight: bold;"><span
    style="color: blue;">W3</span>C</abbr>

    <script type="text/javscript">
    function getText(el)
    {
    if (typeof el.textContent == 'string')
    return el.textContent;
    if (typeof el.innerText == 'string')
    return el.innerText;
    if (typeof el.innerHTML == 'string' )
    return el.innerHTML.replace(/<[^>]*>/g,'');
    return '';
    }
    </script>

    --
    Rob
    RobG, Apr 3, 2007
    #3
    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. Tim T

    do you know why this won't work?

    Tim T, Nov 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,462
    Tim T
    Nov 12, 2003
  2. Marina

    Re: WHY, WHY WON'T IT WORK???

    Marina, Jun 29, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    343
    Marina
    Jun 29, 2004
  3. Chad
    Replies:
    4
    Views:
    8,312
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    862
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page