CES wrote:
All,
Visual Studio 2005 doesn't include an auto complete item for innerHTML
document.getElementById("SomeID").innerHTML, is their a way of
referencing the inner text of an element without using ???.innerHTML,
that will work in all browsers.
innerHTML is a property of an element, its value is as string. I suppose
the only 'logical' auto-complete would be string methods.
The W3C equivalent to innerText is textContent, but that it's not
supported by IE or older browsers - although most modern browsers do.
Probably the most widely supported method is to recurse down an
element's child nodes and grab the data property of text nodes.
But if innerText or textContent are supported, why not use them based on
feature detection? Try:
function getElText(el)
{
if (el.textContent) return el.textContent;
if (el.innerText) return el.innerText;
var x = el.childNodes;
var txt = '';
for (var i=0, len=x.length; i<len; ++i){
if (3 == x[i].nodeType) {
txt += x[i].data;
} else if (1 == x[i].nodeType){
txt += getElText(x[i]);
}
}
return txt.replace(/\s+/g,' ');
}
The following works in Firefox (Win & Mac) but not in IE(Win) or Safari.
document.getElementById("SomeID").firstChild.nodeV alue = "XXX";
It is not a good idea to guess what the firstChild (or any particular
node) will be unless you have complete control over the HTML. And even
then, it makes for brittle code.
--
Rob