Jason S
Jason S

Reputation: 189786

JavaScript + E4X: how to test if an element has text nodes?

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

Upvotes: 1

Views: 300

Answers (1)

Paul Sweatte
Paul Sweatte

Reputation: 24627

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)

Upvotes: 1

Related Questions