0
   <script>
        $('#textarea_').on('keyup', function () {
            var textareaContentWithJquery = $('#textarea_').val();
            var textareaContentWithJavascript = document.getElementById('textarea_').value;
        });
    </script>

        <textarea cols="20" id="textarea_" name="textarea_" rows="2">
Some text in textarea here is created without pressing enter, 
just plain text. But how can i get the position 
of  newline markup from textarea so that i can put <br> there? 
        </textarea>

i get value from textarea like that(plain text); Some text in textarea here is created without pressing enter, just plain text. But how can i get the position of newline markup from textarea so that i can put <br> there?

If I were to press enter in textarea and pass to new line then it would be easy because there would be an \n at the place where I press enter but when I just add text and let the textarea wrap the text to a new line then I have no \n there in the text.

I need it to be like this (not plain text) ;

Some text in textarea here is created without pressing enter,\n
just plain text. But how can i get the position\n
of  newline markup from textarea so that i can put <br> there?\n

So i would easily replace \n with <br> 
Some text in textarea here is created without pressing enter,<br> 
just plain text. But how can i get the position<br> 
of  newline markup from textarea so that i can put <br> there? <br>

textareaContentWithJquery and textareaContentWithJavascript gives the same result i mean without \n markup. So they are useless.

Alex W
  • 37,233
  • 13
  • 109
  • 109
lowdegeneration
  • 359
  • 5
  • 13

2 Answers2

1

You could use &#013; for the new line, and then search for \n and replace with <br>.

 <textarea cols="20" id="textarea_" name="textarea_" rows="12" cols="150">
    Some text in textarea here is created without pressing enter, just plain text. 
    But how can i get the position of newline markup from textarea so that i can put <br>&#013; there? 
 </textarea>

$('#textarea_').on('keyup', function () {
   var textareaContentWithJquery = $('#textarea_').val();                  
   console.log(textareaContentWithJquery.replace(/\n/g, "<br>"));
});

Updated codepen: http://codepen.io/nobrien/pen/QNmOpv

NOBrien
  • 459
  • 3
  • 7
  • nope this doesn't work. Because there is no \n in textareaContentWithJquery. – lowdegeneration Apr 06 '16 at 15:23
  • for instance in the first line type "sometext" and then add one space and then keep adding text and let it pass new line and then check it again because there would be no
    added(except at the very end of text).
    – lowdegeneration Apr 06 '16 at 15:39
  • Sorry,I don't understand what you are saying, or else I don't understand what you are wanting it to do. My codepen is adding br tags anywhere there is a linebreak added before hand or typed. I updated the pen to output the results to the screen, as well as the console. – NOBrien Apr 06 '16 at 15:43
  • make cols=20 and then try again...it would be like "Some text Some text Some text Some text Some text Some text Some text
    " which i can not get line break except at the end of text.
    – lowdegeneration Apr 06 '16 at 15:48
  • Well if you are typing manually, you would just type the return key, not type the encoded variable. Hitting the return key is creating a br tag when I do it. – NOBrien Apr 06 '16 at 16:01
  • but i can not tell user to do so anyway. – lowdegeneration Apr 06 '16 at 16:03
  • Sorry, I guess I don't understand what you are actually trying to do then. Good luck! – NOBrien Apr 06 '16 at 16:06
0

Try this

$('#textarea_').on('keyup', function () {
   var textareaContentWithJquery = $('#textarea_').val();                  
   console.log(textareaContentWithJquery.replace(/\r?\n/g, '<br />'));
});