I need to remove a period inside of a div that only has a class attribute (no id -- system generated page).
<div class="myClass">
Some text .
</div>
I need to remove the period. How can this be accomplished using JavaScript?
Without jQuery, here is the old-school option:
var divs = document.getElementsByTagName('div');
for (var i=0,len=divs.length;i<len;++i){
if (/(?:^|\s)myClass(?:\s|$)/.test(divs[i].className)){
// Removes every period
divs[i].innerHTML = divs[i].innerHTML.replace( /\./g, '' );
}
}
With jQuery:
$('div.myClass').html(function(i,oldHTML){
return oldHTML.replace( /\./g, '' );
});
document.getElementsByClassName('myClass')?document.querySelectorAll(); both of these are not 'old-school', having been implemented only in newer browsers.innerHTML, which has all kinds of potential problems if the <div> contains other elements.innerHTML is generally dangerous, is used blindly by many developers and I'd prefer to see warnings when it's being encouraged.If you're using jQuery, you can do this:
$(".myClass").html(function(i, html) { return html.replace(/\.$/, ""); });
html() (as in my answer) or var a = $('...'); a.html(a.html().replace(...));