John Magnolia John Magnolia - 3 months ago 10
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 - https://codepen.io/anon/pen/WxWgZL

$($(this).html()).is('i');

$.trim($($(this).html()).remove('i')));


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

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

Answer
$("button").each(function(index,element){
    console.log($(this).children().context.childNodes);
    // here you can check if the array of element contains "text" element in it
});