Home > other >  How can I replace specific characters in a div?
How can I replace specific characters in a div?

Time:01-26

I would like to change the comma into an apostrophe in this div.

<div id="range" >   
  <div >15,000.00</div>
</div> 

So instead of 15,000.00 I would like to do 15'000.00

I cannot alter the HTML so I think I need to achieve this in JS, right?

Do I need to use the changeText function?

Many thanks in advance.

CodePudding user response:

There is no native changeText function in JavaScript. You can change the text content of an element using textContent property and use replace() method to replace , to '.

let elem = document.querySelector(".something")

elem.textContent = elem.textContent.replace(",", "'");
<div id="range" >   
  <div >15,000.00</div>
</div> 

CodePudding user response:

To add to TechySharnav's answer, you can also query for the classname directly.

var el = documents.getElementByClassNames("something");
el.textContent = el.textContent.replace(',',"'");
  •  Tags:  
  • Related