Why won't this work

S

sjoshi

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
 
E

Evertjan.

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]
 
R

RobG

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top