Barnala   

Remove from html

I want to remove  

from below code.

<div class="country-IN">
   <span class="locality">Barnala</span>
   &nbsp;&nbsp;
   <span class="state">Punjab</span>
   &nbsp;&nbsp;
   <span class="country">India</span>
</div>

      

Please help me get out of it.

+1


source to share


5 answers


I would suggest:

var el = document.querySelector('.country-IN');
el.innerHTML = el.innerHTML.replace(/&nbsp;/g,'');

      

JS Fiddle demo .

Or with jQuery:

$('.country-IN').html(function(i,h){
    return h.replace(/&nbsp;/g,'');
});

      



JS Fiddle demo .

Or even:

$('.country-IN').children().each(function(i,e){
    this.parentNode.removeChild(this.nextSibling);
});

      

JS Fiddle demo .

Though it would be easier to just edit the HTML files themselves and just remove those character lines.

+13


source


You can get the elements you want and add them as html for the element

$('.country-IN').html(function(i,v){
   return $('<div>').append($(this).children('*')).html(); 
});

      



FIDDLE

+1


source


Library Agnostic:

(function() {
var country_els = document.querySelectorAll('.country-IN');

for (var i = 0; i < country_els.length; i++) {
    for (var j = 0; j < country_els[i].childNodes.length; j++) {
        var node = country_els[i].childNodes[j];
        if (typeof node.tagName === 'undefined') {
            node.parentNode.removeChild(node);
        }
    }  
}
})();

      

Fiddle

0


source


First, get the entire HTML div file and convert it to string

convertHtmlToText(str)
{
   str = str.toString();
  return str.replace(/<[^>]*(>|$)|&nbsp;|&zwnj;|&raquo;|&laquo;|&gt;/g, ' ');
}

      

you will get text without HTML tag and & nbsp etc.

You can add multiple conditions in the above solution

0


source


You can do this with preg_replace in PHP.

<?php
$html = "YOUR HTML"
$new = preg_replace("!&nbsp;!", "", $html);
print($new);
?>

      

I've used this script before and it should work fine.

-1


source







All Articles