Here's a quick DOM-compliant function for getting the XSLT-like node value for any element or text node in the DOM. (nodeType property returns 1 for elements and 3 for text nodes)

function getText(element) {
  if (element.nodeType == 1) {
    var concatenation = '';
    for(var i = 0; i < element.childNodes.length; i++) {
      concatenation += getNodeValue(element.childNodes.item(i));
    }
    return concatenation;
  } else if (element.nodeType == 3) {
    return this.nodeValue;
  }
}

Derek Broughton wrote:
On Wednesday 22 December 2004 16:42, Jeff Beal wrote:

The only thing that I always forget is that nodeValue() only returns a
value for text nodes.  It's not like XSLT where the text value of an
element is the text value of all contained text nodes.  So, basically,
this.nodeValue wouldn't have worked anyway.  The DOM-compliant way of
doing this.innerText (for an element with only one child node) would be
this.childNodes.items(0).nodeValue.  this.innerText is a lot neater.


Darn. MS's DHTML reference says that there's no public standard for innerText, which means it's only guaranteed to work on IE.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to