How to replace a word in javascript?

Asked

Viewed 27 times

-2

How to replace the word "Minutes" with "Minutes" inside the div.info-box?

<div class="info-box">
  <span class="date-info">13 de julho de 2021</span>
  <span class="separator-symbol">•</span>
  3 Minutes
</div>

I tried to use this syntax to identify the word to be replaced and perform its substitution, but it didn’t work:

document.querySelector('.info-box').replace('Minutes' , 'Minutos');

I also tried using . innerText but it replaces everything inside the div.info-box, including spans.

  • 1

    To analyze: what is the function return type querySelector? This type allows a replace?

1 answer

1


You can use replace together with Element.innerText

Thus:

var text = document.querySelector(".info-box")

text = text.innerText.replace("Minutes", "Minutos")

document.querySelector('.info-box').innerText = text
<div class="info-box">
  <span class="date-info">13 de julho de 2021</span>
  <span class="separator-symbol">•</span>
  3 Minutes
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.