John Magnolia John Magnolia - 7 months ago 31
jQuery Question

Check if element only contains another element without any text

E.g: a button only contains icon

<button><i>i</i> Foo</button> -- false

<button><i>i</i></button> -- true

<button> <i>i</i> </button> -- true

I've tried this so far -



Here's what I ended up with thanks to @veerasuthan V

var children = $(this).children().context.childNodes;
for(i = 0; i < children.length; i++) {
if (children[i] && children[i]['nodeType'] == 3) {
return false;
return true;

    // here you can check if the array of element contains "text" element in it