Last Updated: February 25, 2016
·
4.148K
· faceleg

Gotcha: jQuery's text() & nbsp

$('<p>&nbsp;</p>').text().trim(); // == ''
$('<p> </p>').text().trim(); // == ''

Just a warning for those of you that thought nbsp; might be treated differently in this situation: it isn't.

If you need to check that an element is empty apart from nbsp, you could use:

var element = $('<p>&nbsp;</p>');
!/&nbsp;/.test(p.html()) && p.text().trim() == ''; // == false