Search code examples
htmlangular5enter

To replace enter key press event with \n


textarea.html

    <textarea [(ngModel)]="array" (keypress)="onKeypress($event)"></textarea>
    <div>
      <p>{{array}}</p>
    </div>

textarea.ts

rray:any;

  constructor() { 
    this.array =["test1", "test2", "test3"];
    // let array2 = ((array).toString()).split("");

    // let array3 = array.join("textarea"); 


  }

  ngOnInit() {
  }

  onKeypress(event){

    if (event.key == "Enter"){
    this.array =  this.array + "\n";
      return false;
    }
  else{
    return true;
  }
  }

}

How to print "\n" when enter key is pressed in the textarea using angular 5? The above code does not work.


Solution

  • actually the error was that I had use "/\n" instead of "\n" as \n is a special character. After doing this it worked