-6

i Want to Remove unnecessary space and write content in separated line in textarea.
Ex.

     abc
        abcde
   abd

Convert it to:

abc
abcde
abd

Amy
  • 4,034
  • 1
  • 20
  • 34
Umesh Soni
  • 11
  • 4

1 Answers1

1

You need to do two things:

  1. Grab the content from the TextArea
  2. Loop through the lines and trim each one (removing any empty lines)

CODE:

var content = myTextArea.value;

//Split into lines
var lines = content.split( /\r?\n/ );

//Your new content
var newContent = "";

//Loop through all lines
for ( var i = 0; i < lines.length; i++ )
{
    //Trim it first
    var line = lines[ i ].trim();

    //Empty line, remove it
    if ( line === "" ) continue;

    //Add to new content
    newContent += line + "\n";
}

//Save to text area
myTextArea.value = newContent;
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330