PRANAV
PRANAV

Reputation: 1111

How to remove a div which contains specific text by JQuery

I am trying to remove a div which contains   and space. it's not working please help

<div id="contentrow" class="contentrow_afterlhsrhs">
&nbsp;                  </div>
if($('.contentrow_afterlhsrhs').html() == "&nbsp;                   ") {
 $('.contentrow_afterlhsrhs').remove();
}

http://jsfiddle.net/L8dwn/141/

Upvotes: 0

Views: 576

Answers (4)

John
John

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 &nbsp; and space inside your div.

<!-- This will not remove your div -->
<div id="contentrow" class="contentrow_afterlhsrhs">&nbsp;</div>

And

<!-- This will remove your div -->
<div id="contentrow" class="contentrow_afterlhsrhs">&nbsp;              </div>

Here is the JavaScript code :

var myContent = $('.contentrow_afterlhsrhs').html();
var trimContent = $.trim(myContent);
var contentWihoutNbsp = myContent.replace('&nbsp;','');

if( trimContent == "&nbsp;" && contentWihoutNbsp ){ 
    $('.contentrow_afterlhsrhs').remove();
}

Upvotes: 1

Nicolai Ehemann
Nicolai Ehemann

Reputation: 574

Simpler approach:

$('.contentrow_afterlhsrhs:contains("\u00a0                     ")').remove();

(you have to use the unicode representation of &nbsp; for the contains() filter).

Edit: JSFiddle: https://jsfiddle.net/L8dwn/144/

Upvotes: 0

KavitaP
KavitaP

Reputation: 61

Please check the working code below :

console.log("Before Remove :" + $('.contentrow_afterlhsrhs').html());
if($.trim($('.contentrow_afterlhsrhs').html()) == "&nbsp;") {
  $('.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">
&nbsp;                  
</div>

Upvotes: 0

Rahul Patel
Rahul Patel

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()) == "&nbsp;") {
    $('.contentrow_afterlhsrhs').remove();
}

Upvotes: 0

Related Questions