Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Replace spaces with dashes in an HTML class or id using javascript

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!