0

I am trying to replace the spaces of the innerHTML of an element by the following code:

<script type="text/javascript"> var minsFix = document.getElementById("spaceX").innerHTML; var keepVal = minsFix.replace(/\s+/g, "-"); document.getElementById("spaceX").innerHTML = keepVal; </script> 

But it only changes the first space to a dash(-) and other spaces are not changed respectively.

Resolved I manage to create my solved code. here

<script type="text/javascript"> // create variable for collect Class : spaceX var keepVal = document.getElementsByClassName("spaceX"); // loop for collect SpaceX for (var i = 0; i < keepVal.length; i++) { // turn spaceX collected to Text var tranText = keepVal[i].innerText; // create variable for replace spaceX to - var toSpace = tranText.replaceAll(/\s+/g, '-'); // output to html one by one document.getElementsByClassName('spaceX')[i].innerHTML = toSpace; } </script> 

hope can help someone

5
  • 1
    You cannot have more than element with that id; it must be unique. Commented Apr 28, 2022 at 12:56
  • @connexo can i change to by other method with class or something like that Commented Apr 28, 2022 at 13:40
  • Sure, use getElementsByClassName or querySelectorAll, and then loop over the nodes those return. Commented Apr 28, 2022 at 13:49
  • Can you provide a runnable snippet (with the relevant HTML) that reproduces the issue? Commented Apr 28, 2022 at 13:51
  • .replaceAll(' ', '-') Commented Apr 28, 2022 at 14:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.