1

In E4X, I know how to test if an element has a particular attribute, but how do I test if an element has text nodes?

js>x = <foo><bar /><baz attr1="123" /><quux>some random text</quux></foo>
<foo>
  <bar/>
  <baz attr1="123"/>
  <quux>some random text</quux>
</foo>
js>'@attr1' in x.baz
true
js>'@attr1' in x.quux
false
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Jason S
  • 184,598
  • 164
  • 608
  • 970

1 Answers1

1

Use the hasSimpleContent method for elements without attributes or children:

x.quux.hasSimpleContent()

Use a RegExp to remove the tags and whitespace to aggregate the text nodes:

Boolean(x.toString().replace(/<.*?\>/g,"").replace(/\W/g,"").length)
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265