-7

how to remove spaces. i use onkeyup to show output in div

Thanks

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

inputBox.onkeyup = function() {
  document.getElementById('printchatbox').innerHTML = inputBox.value;
}
<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>

3
  • 1
    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Apr 5, 2018 at 13:53
  • 1
    Were you unable to find any information about this? Commented Apr 5, 2018 at 13:54
  • document.getElementById('printchatbox').innerHTML = inputBox.value.replace(/\s/g,""); Commented Apr 5, 2018 at 13:54

2 Answers 2

0

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>

Sign up to request clarification or add additional context in comments.

Comments

0

Use replace

  document.getElementById('printchatbox').innerHTML = inputBox.value.replace(/\s/g,"");

Demo

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.