Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Why won't this work

Reply
Thread Tools

Why won't this work

 
 
sjoshi
Guest
Posts: n/a
 
      04-03-2007
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[i];
//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

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      04-03-2007
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];


I doubt that, not under IE7.

Use:

var currentAbbr = document.getElementsByTagName('abbr')[i];


> //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)
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-03-2007
On Apr 3, 8:11 pm, "Evertjan." <(E-Mail Removed)> 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

 
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
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
Re: WHY, WHY WON'T IT WORK??? Marina ASP .Net 2 06-29-2004 02:40 PM
Why oh why doesn't my data view work? David Prowak ASP .Net 1 01-30-2004 04:19 AM



Advertisments