0

When the user presses enter in the input box I would like that to reflect in the H1 tag.

As you can see when the user types Line 1 they can press enter and create line 2 in the input no problem, but the output in the h1 tag is still "Line 1 line 2" all on one line.

Please look at the fiddle as that will make a lot more sense.

I was thinking about using a keypress function to get when the user presses enter and then do something with that? I'm not sure if there is a better way though.

https://jsfiddle.net/BSmyth634/ovz8thrp/

<div ng-app>
   <form>
     <textarea rows="4" type="text" ng-model="todoText" size="30" placeholder="add new todo here">
     </textarea>
   </form>
   <h1>
    {{todoText || 'Hello World'}}
   </h1>
</div>
Brad
  • 99
  • 11

4 Answers4

1

You need to use style="white-space: pre;" in your code to do it.

JS Fiddle

Techidiot
  • 1,921
  • 1
  • 15
  • 28
0

I see two ways to achieve what you want.

  • Use white-space css property. In your case the pre-line value might be interesting.
  • Create Angular's filter and use it.
0

you can use pre element instead of h1

<pre>{{todoText || 'Hello World'}}</pre>

or use this

<h1 ng-repeat="line in todoText.split('\n')">
    {{line}}
</h1>
fingerpich
  • 8,500
  • 2
  • 22
  • 32
0

Jquery approach

$("textarea").on('keypress',function(e){
    if(e.keyCode == 13){
        $('h1').html($("textarea").val())
    }

})
Akshay
  • 815
  • 7
  • 16