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 />"));
}
Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
M. Dhaouadi
  • 569
  • 8
  • 26

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>
Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94