By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

a function like nl2br but in javascript?

P: 42
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
Share this Question
Share on Google+
2 Replies

Expert 5K+
P: 5,821
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

P: 42
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.