-1

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

<div id="range" class="range-slider">   
  <div class="something">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.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Tycin
  • 1
  • Does this answer your question? [How to replace all occurrences of a string in JavaScript](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) | [How do I replace text inside a div element?](https://stackoverflow.com/questions/121817/how-do-i-replace-text-inside-a-div-element/18723036) – isherwood Jan 25 '22 at 14:11

2 Answers2

3

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" class="range-slider">   
  <div class="something">15,000.00</div>
</div> 
TechySharnav
  • 4,869
  • 2
  • 11
  • 29
  • I tried this solution but I get an error Uncaught TypeError: elem is null – Tycin Jan 25 '22 at 14:13
  • @Tycin what query did you use inside the `querySelector`? You need to put a `.` before the class name. – TechySharnav Jan 25 '22 at 14:22
  • Yep, this is what I used. In my case – Tycin Jan 25 '22 at 14:36
  • @Tycin make sure you load the JavaScript after loading the body i.e put JavaScript after closing body tag. Also make sure, element with class `noUi-tooltip` exists. – TechySharnav Jan 25 '22 at 14:51
0

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

var el = documents.getElementByClassNames("something");
el.textContent = el.textContent.replace(',',"'");
will
  • 108
  • 1
  • 6
  • This will only work if OP wants to replace the text in the **first** occurring element that matches the `.something` selector. – Terry Jan 25 '22 at 14:03