• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Replace a Space with an Underscore

Using JavaScript to Replace a Space with an Underscore

February 27, 2022 Leave a Comment

To replace a space with an underscore in a paragraph using JavaScript, we can use the JavaScript String replace() method.

text.replace(/ /g, '_');

Let’s say we have the following HTML:

<div id="div1">
  <p id="p1">We will update the text: <span id="span1">text with underscore</span></p>
</div>

If we want to change the spaces in the span “text with underscore” and replace them with underscores, we will use the replace method in the following JavaScript code:

var text = document.getElementById("span1").textContent.replace(/ /g, '_');
document.getElementById("span1").innerHTML = text;

Which would result in the following:

We will update the text: text_with_underscore

Using JavaScript to Replace a Space with an Underscore with a Click

In this example, we will let the user input any text they want, and then replace any spaces in the text with underscores(_). We will then display the new text below. If there are no spaces in the text provided, no change will occur.

Here is the HTML setup.

<p>Add text below with spaces.</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="changeText()" value="View result">
<div id="results"></div>

Below is the JavaScript code which will take the user input using an onclick event and run the function below. The function will get the text using the getElementById method and textContent property, and use the replace() method on the text. We will then update the #results div using the textContent property.

Here is the JavaScript code:

function changeText() {
  var userText = document.getElementById("userVal").value;
  var replacedText = userText.replace(/ /g, '_');
  document.getElementById("results").textContent = replacedText;
}

The final code and output for this example of how to replace spaces with underscores using JavaScript is below:

Code Output:

Add text below with spaces.


Full Code:

<p>Add text below with spaces.</p>
<input id="userVal" type="text" value="">
<input id="submitNum" type="submit" onclick="changeText()" value="View result">
<div id="results"></div>

<script>

function changeText() {
  var userText = document.getElementById("userVal").value;
  var replacedText = userText.replace(/ /g, '_');
  document.getElementById("results").textContent = replacedText;
}

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to replace a space with an underscore.

Other Articles You'll Also Like:

  • 1.  JavaScript Change Background Color of Element
  • 2.  Remove Undefined Values From an Array in JavaScript
  • 3.  How to Check if a String Contains Vowels in JavaScript
  • 4.  Using JavaScript to Get a Random Number Between 1 and 5
  • 5.  How to Hide a Div in JavaScript
  • 6.  JavaScript Coin Flip – How to Simulate Flipping a Coin in JavaScript
  • 7.  Using JavaScript to Set Select to the First Option
  • 8.  Using JavaScript to Check If String is a Number
  • 9.  JavaScript Round to Nearest 10
  • 10.  Using JavaScript to Get a Random Number Between Range of Numbers

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy