1

I have a site with French and English dimensions. For example, I have an image with the dimensions 36 x 46.5 cm. Since it's French, the dot (".") should be a comma (",").

The structure:

<div class="wrapper">
  <span class="custom-field">46.5</span>
</div>

My code:

<script>
var period = document.getElementsByClassName("custom-field");
var comma = period.replace(".",",");
document.getElementsByClassName("custom-field").innerHTML = comma
</script>

The console tells me that period.replace(".",",") is not a function. Please help and thanks in advance.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
user42760
  • 85
  • 1
  • 7
  • 1
    because `period` is an HTML Collection, not text. – epascarello Jul 08 '20 at 18:14
  • Does this answer your question? [What do querySelectorAll and getElementsBy\* methods return?](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return) – SMAKSS Jul 08 '20 at 18:22

8 Answers8

1

The period is actually not a string it is an array of HTMLElements, so if you want to use replace method it should be on string. You can access your span data like this:

var period = document.getElementsByClassName("custom-field")[0].innerHTML;
var comma = period.replace(".",",");

if you want to change all of them then you can do like this:

var customFields = document.getElementsByClassName("custom-field");
for (let i = 0; i < customFields.length; i++) {
 customFields[i].innerHTML = customFields[i].innerHTML.replace(".",",") 
};
Evgeny Klimenchenko
  • 1,184
  • 1
  • 6
  • 15
1

Since document.getElementsByClassName results HTMLCollection, you need to loop it through.

Try like this:

var period = document.getElementsByClassName("custom-field");

for (let item of period) {
  item.innerHTML = item.innerHTML.replace(".", ",");
}
<div class="wrapper">
  <span class="custom-field">46.5</span>
</div>
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
1

Use document.querySelector to get the first element matching a selector and use .textContent to get its text.

var el = document.querySelector('.custom-field');
el.textContent = el.textContent.replace(".", ",");
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
1

Working solution with comments below..

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<div class="wrapper">
  <span class="custom-field">46.5</span>
</div>
</body>
<script>
    //Changed to querySelector rather than elementByClassName
    var period = document.querySelector(".custom-field");

    //You forgot to add the inner text of element you needed to replace
    var comma = period.innerHTML.replace(".", ",");
    console.log(comma);
    period.innerHTML = comma
</script>
</html>
StrayAnt
  • 369
  • 2
  • 7
0

You need to extract the text from the first element (add [0].innerHTML to the initialization of period.

var period = document.getElementsByClassName("custom-field")[0].innerHTML;
var comma = period.replace(".",",");
document.getElementsByClassName("custom-field")[0].innerHTML = comma;
<div class="wrapper">
  <span class="custom-field">46.5</span>
</div>
Musab Guma'a
  • 175
  • 2
  • 9
0

This is because period is a collection of HTML elements. We need to get the innerHTML of the first([0]) element. Try the following code :-

<script>
var period = document.getElementsByClassName("custom-field")[0].innerHTML;
var comma = period.replace(".",",");
document.getElementsByClassName("custom-field").innerHTML = comma
</script>
Suyash Kumar
  • 113
  • 1
  • 8
0

Can you do this.

var elem = document.querySelector(".custom-field");

elem.innerText = el.innerText.replace('.', ',');
<div class="wrapper">
  <span class="custom-field">46.5</span>
</div>
GMKHussain
  • 3,342
  • 1
  • 21
  • 19
0

probably you not need to think about replacing. Try to output number values with browser localization.

on javascript, number value can be localized:

<div>
  <h2>Lorem ipsum and so on...</h2>
  your number value is 46.5. 
  Localized presentation is:&nbsp;<span class="localized-number" data-value="46.5"></span>
</div>
<script>
// self executing function here
(function() {
  document.querySelectorAll('.localized-number').forEach((domElement) => {
    domElement.innerText = Number(domElement.getAttribute('data-value')).toLocaleString()
  });
})();
</script>
Sergio Belevskij
  • 2,478
  • 25
  • 24