1

when I press Enter I want to insert "< br >" instead of the new line '\n\r'in my text area, Exemple:

i want the text in the text area to be:

"hello <br> dear", 

instead of

"hello
dear"

I tried this code but with no luck:

$('#inputText').bind('keyup', function(e) {
   var data = $('#inputText').val();
   $('#inputText').text(data.replace(/\n/g, "<br />"));
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
M. Dhaouadi
  • 617
  • 10
  • 27

1 Answers1

1

Your code will work fine if:-

1.bind() converted to on()(because bind() is deprecated)

2.text() need to be .val()

Working example (check comments too):-

// applied mouseout to prevent repetition on each key-press

//you can apply keyup also no problem

$('#inputText').on('mouseout', function(e) {
  var data = $('#inputText').val();
  $('#inputText').val(data.replace(/\n/g, "<br />")); // text() need be val()
}); // here ); missing in your given code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<textarea id="inputText"></textarea>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98