Dave Thompson
Dave Thompson

Reputation: 21

JavaScript Replace in Class

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?

Upvotes: 2

Views: 146

Answers (2)

user193476
user193476

Reputation:

If you're using jQuery, you can do this:

$(".myClass").html(function(i, html) { return html.replace(/\.$/, ""); });

Upvotes: 1

Phrogz
Phrogz

Reputation: 303253

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, '' );
});

Upvotes: 4

Related Questions