[Solved] How to remove spaces using JavaScript? [duplicate]


You can use replace to replace spaces with nothing.

var inputBox = document.getElementById('chatinput');

inputBox.onkeyup = function() {
  document.getElementById('printchatbox').innerHTML = inputBox.value.replace(' ', '');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <label>Title Page</label>
  <input type="text" id="chatinput" class="form-control" required="">
</div>
<div class="form-group">
  <div><b>Permalink: </b>http://doamin.com/<span id="printchatbox" class="cl-blue"></span></div>
</div>

NOTE:

Upon seeing a suggested edit, I must remark that this is NOT the correct answer, because it takes care of one space only. If there are multiple spaces in the string, it will remove only the first one. As the editor suggested, the right way to do this is by using a regex. In that case, replace would replace all spaces

var inputBox = document.getElementById('chatinput');

inputBox.onkeyup = function() {
  document.getElementById('printchatbox').innerHTML = inputBox.value.replace(/\s/g, '');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <label>Title Page</label>
  <input type="text" id="chatinput" class="form-control" required="">
</div>
<div class="form-group">
  <div><b>Permalink: </b>http://doamin.com/<span id="printchatbox" class="cl-blue"></span></div>
</div>

solved How to remove spaces using JavaScript? [duplicate]