lowdegeneration
lowdegeneration

Reputation: 379

newline markup in textarea

   <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.

Upvotes: 0

Views: 305

Answers (2)

NOBrien
NOBrien

Reputation: 459

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

Upvotes: 1

Arokiyanathan
Arokiyanathan

Reputation: 94

Try this

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

Upvotes: 0

Related Questions