Chonchol Mahmud Chonchol Mahmud - 5 months ago 14
jQuery Question

How to insert a space before the every sentence in JavaScript?

My Input is:

hello world one
hello world two
hello world three


I have tried so far:

$('.insertSpace').click(function(event) {
var textareaInput=$('.textareaInput').val();
var myString = ' ' + textareaInput.split();
$('.textareaInput').val(myString);
//console.log(myString);
});


It is working for just first sentence.I want to insert space in every sentence. Where is the fault in my code?

Its output should look like this:

hello world one
hello world two
hello world three

Answer
var myString = textareaInput.split("\n").map(function(str){ 
  return ' ' + str;
}).join("\n");
Comments