I want to remove
from the below code.
<div class="country-IN">
<span class="locality">Barnala</span>
<span class="state">Punjab</span>
<span class="country">India</span>
</div>
Please help me to get out of it.
I want to remove
from the below code.
<div class="country-IN">
<span class="locality">Barnala</span>
<span class="state">Punjab</span>
<span class="country">India</span>
</div>
Please help me to get out of it.
Share Improve this question edited Jul 22, 2020 at 21:45 doğukan 27.4k13 gold badges62 silver badges75 bronze badges asked Mar 11, 2013 at 18:14 Manjit SinghManjit Singh 8744 gold badges9 silver badges12 bronze badges 2- remove it how? Can't you just open the file and hit delete? – Ryan B Commented Mar 11, 2013 at 18:16
- This may be a code snippet stored in a CMS... – Marc Audet Commented Mar 11, 2013 at 18:17
6 Answers
Reset to default 18I'd suggest:
var el = document.querySelector('.country-IN');
el.innerHTML = el.innerHTML.replace(/ /g,'');
JS Fiddle demo.
Or, with jQuery:
$('.country-IN').html(function(i,h){
return h.replace(/ /g,'');
});
JS Fiddle demo.
Or even:
$('.country-IN').children().each(function(i,e){
this.parentNode.removeChild(this.nextSibling);
});
JS Fiddle demo.
Though it'd be easier to simply edit the HTML files themselves and just remove those strings of characters.
You can get the elements you want and add it back as the html for the element
$('.country-IN').html(function(i,v){
return $('<div>').append($(this).children('*')).html();
});
FIDDLE
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
First, get your whole HTML div and convert it to the string
convertHtmlToText(str)
{
str = str.toString();
return str.replace(/<[^>]*(>|$)| |‌|»|«|>/g, ' ');
}
you will get the text without HTML tag and   etc
you can add multiple conditions in the above solution
We don't need to javascript for that. We can hide the
with only CSS. Add font-size: 0
to container and font-size: 1rem
to child.
.second {
font-size: 0;
}
.second > span {
font-size: 1rem;
}
<div class="country-IN">
<span class="locality">Barnala</span>
<span class="state">Punjab</span>
<span class="country">India</span>
</div>
<div class="country-IN second">
<span class="locality">Barnala</span>
<span class="state">Punjab</span>
<span class="country">India</span>
</div>
You can do this with preg_replace in PHP.
<?php
$html = "YOUR HTML"
$new = preg_replace("! !", "", $html);
print($new);
?>
I used this script before and it should work fine.