Reputation: 7734
i have a little problem with my script. I need to check some span, if i have some content in the middle of it i do nothing... but if it's empty i want to remove this span or for example change style. Now when specify span is empty script cleans all of my spans, even their are not empty. Can you help me?
html
<span class="insert_list_item"></span>
<span class="insert_list_item">data</span>
script
if ($('.insert_list_item').length > 0){
$('.insert_list_item').each().remove();
}
Upvotes: 1
Views: 93
Reputation: 123377
Use :empty
$('.insert_list_item:empty').remove()
to remove all empty elements. Or
$('.insert_list_item:not(:empty)').remove()
to remove all non-empty elements
On http://api.jquery.com/category/selectors/ you can see all selectors available.
Upvotes: 4
Reputation: 207501
each checking length:
$('.insert_list_item').each( function(){
var span = $(this);
if (span.html().length === 0) {
span.remove();
}
});
or
filter with :empty:
$('.insert_list_item').filter(":empty").remove();
Upvotes: 2
Reputation: 185923
How about:
$( '.insert_list_item' ).filter(function () {
return $.trim( $( this ).text() ) === '';
}).remove();
Live demo: http://jsfiddle.net/RMZLm/
Note that this solution also works on SPAN elements that contain white-space (e.g. <span class="insert_list_item"> </span>
).
Upvotes: 2
Reputation: 659
try this:
if ($('.insert_list_item').text().length > 0){
$('.insert_list_item').remove();
}
Upvotes: 0
Reputation: 148110
Use condition inside each
. As each iterates through all the elements returned by selector.
$('.insert_list_item').each(function(){
if($(this).html().length === 0)
$(this).remove();
});
Upvotes: 1