5h0-xg
Last Updated: February 25, 2016
·
1.93K
· faceleg
98aba6d508f3f1e7fd6b21e8bfdb6b6c

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
Say Thanks
Respond