Connecting Tech Pros Worldwide Forums | Help | Site Map

Why won't this work

sjoshi
Guest
 
Posts: n/a
#1: Apr 3 '07
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


Evertjan.
Guest
 
Posts: n/a
#2: Apr 3 '07

re: Why won't this work


sjoshi wrote on 03 apr 2007 in comp.lang.javascript:
Quote:
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.
Quote:
//This works, I get an element node
var currentAbbr = abbrs[i];
I doubt that, not under IE7.

Use:

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

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

works just as nice.
Quote:
//*** This fails ***
var abbrText = currentAbbr.lastChild.nodeValue;
works fine here [IE7]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
RobG
Guest
 
Posts: n/a
#3: Apr 3 '07

re: Why won't this work


On Apr 3, 8:11 pm, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
Quote:
sjoshi wrote on 03 apr 2007 in comp.lang.javascript:
>
Quote:
I have this HTML fragment
>
Quote:
The <abbr title="World Wide Web Consortium">W3C</abbr>
>
Quote:
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
[...]
Quote:
Quote:
//*** 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

Closed Thread