Consider the JavaScript below:
var v;
if (this.children.length > 0) {
v = this.firstElementChild.value;
}
This works in modern versions
this.firstElementChild
should work in every significant browser bar IE <=9 and Firefox 3 (QuirksMode).
this.children[0]
will work in every significant browser bar Firefox 3, except that IE <=9 counts comment nodes as element nodes (QuirksMode). This may or may not be an issue for you.
The catch-all system is this:
var node = this.firstChild,
firstElementChild = null;
for ( ; node; node = node.nextSibling) {
if (node.nodeType === 1) {
firstElementChild = node;
break;
}
}
firstElementChild
will then be the first element child if one exists, null
otherwise. It would be best to see if this.firstElementChild
exists before doing the loop, for performance reasons.