468,484 Members | 2,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

a function like nl2br but in javascript?

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
2 9229
pbmods
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);
  2.  
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');
  2.  
  3. $textarea.value = $textarea.vaue.replace('\n', '<br />\n');
  4.  
Aug 13 '07 #2
Gozil
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..

edit:
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
reply views Thread by NPC403 | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.