Gotcha: jQuery's text() & nbsp
$('<p> </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> </p>');
!/ /.test(p.html()) && p.text().trim() == ''; // == false
Written by Michael Robinson
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Jquery
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#