471,311 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,311 software developers and data experts.

a function like nl2br but in javascript?

Hello, was just wondering if its possible to have a function like nl2br in javascript. I need it to change the row where the user have used enter.
Aug 13 '07 #1
2 9346
5,821 Expert 4TB
Heya, Gozil.

If you think about it, nl2br() is really just a fancy way of doing this:
Expand|Select|Wrap|Line Numbers
  1. str_replace("\n", "<br />\n", $str);
It's actually slightly more complicated than that, but the point is the same.
With that in mind, you can run a quickie string replace of your own:

Expand|Select|Wrap|Line Numbers
  1. var $textarea = document.getElementById('theTextarea');
  3. $textarea.value = $textarea.vaue.replace('\n', '<br />\n');
Aug 13 '07 #2
Thanks, but i need to use it in a xml file and if I use <br /> in another xml tag i wont get the whole node. But I could use something like [br] and then convert it to <br /> later. But im not sure how to convert back a text that uses [] I get it wrong when I write this:
Expand|Select|Wrap|Line Numbers
  1. var my_string = 'hello this is a text[br]this is the next row';
  2. my_string = String.replace(my_string,/[br]/g,'<br />');
it doesnt replace the whole [br] tag..

nvm I can just use this when i try to output the text:
my_string = String.replace(my_string,/[\n]/g,'<br />');

instead of replacing the \n before i put it in the mySQL database.
Aug 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by kenneth | last post: by
2 posts views Thread by Indrid Cold | last post: by
3 posts views Thread by Matthew Sims | last post: by
6 posts views Thread by leegold2 | last post: by
11 posts views Thread by Jakanapes | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.