Reputation: 1111
I am trying to remove a div which contains and space. it's not working please help
<div id="contentrow" class="contentrow_afterlhsrhs">
</div>
if($('.contentrow_afterlhsrhs').html() == " ") {
$('.contentrow_afterlhsrhs').remove();
}
http://jsfiddle.net/L8dwn/141/
Upvotes: 0
Views: 576
Reputation: 4981
Here is the fiddle :
http://jsfiddle.net/L8dwn/143/
You can try it by removing space and adding space. This will remove your div only if there is the couple of
and space inside your div.
<!-- This will not remove your div -->
<div id="contentrow" class="contentrow_afterlhsrhs"> </div>
And
<!-- This will remove your div -->
<div id="contentrow" class="contentrow_afterlhsrhs"> </div>
Here is the JavaScript code :
var myContent = $('.contentrow_afterlhsrhs').html();
var trimContent = $.trim(myContent);
var contentWihoutNbsp = myContent.replace(' ','');
if( trimContent == " " && contentWihoutNbsp ){
$('.contentrow_afterlhsrhs').remove();
}
Upvotes: 1
Reputation: 574
Simpler approach:
$('.contentrow_afterlhsrhs:contains("\u00a0 ")').remove();
(you have to use the unicode representation of
for the contains() filter).
Edit: JSFiddle: https://jsfiddle.net/L8dwn/144/
Upvotes: 0
Reputation: 61
Please check the working code below :
console.log("Before Remove :" + $('.contentrow_afterlhsrhs').html());
if($.trim($('.contentrow_afterlhsrhs').html()) == " ") {
$('.contentrow_afterlhsrhs').remove();
}
console.log("After Remove :" +$('.contentrow_afterlhsrhs').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contentrow" class="contentrow_afterlhsrhs">
</div>
Upvotes: 0
Reputation: 5246
use $.trim() function of jquery to remove unnecessary spaces from string. After fetching .html() of div trim the spaces and compare it with a string.
if($.trim($('.contentrow_afterlhsrhs').html()) == " ") {
$('.contentrow_afterlhsrhs').remove();
}
Upvotes: 0