John Magnolia John Magnolia - 1 year ago 63
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;

Answer Source
    // here you can check if the array of element contains "text" element in it
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download