Monday, August 31, 2015

Add Spaces using Javascript

Problem

Create a textbox and a button. When button is clicked, replace the text in the textbox, with spaces between each character in the original text.

Note: window.alert was used to debug

Solution

<!doctype html>
<html>
 Input Text: <input id='inText' type='text'>
 <button onClick='addSpaces();'>Click to add spaces :)</button> 
</html>

<script language='javascript' type='text/javascript'>
function addSpaces() {
  var text = document.getElementById('inText');
  var newtext = text.value.split('').join(' ');
  //window.alert(newtext);
  text.value = newtext; 
}
</script>

No comments:

Post a Comment