SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: nl2br equivalent
-
Nov 6, 2003, 15:43 #1
- Join Date
- Mar 2002
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
nl2br equivalent
Is there a javascript equivalent of PHP's nl2br() function? I have to send the value of a textarea to a new window that will display the text of what was in the box, which is why I need to somehow convert newlines to breaks.
Thanks,
Greg
-
Nov 6, 2003, 16:00 #2
There's nothing built-in, but you can write your own nl2br function:
Code:function nl2br(someText) { return someText.replace("\n", "<br />"); }
-
Nov 6, 2003, 22:19 #3
- Join Date
- Mar 2002
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks!
That seemed to work, but for the first linefeed only. I figured out the solution is to make a loop that repeats the function until the end of the textarea.
-
Nov 6, 2003, 22:44 #4
- Join Date
- May 2002
- Location
- Relative
- Posts
- 452
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Gregory
Code:function nl2br(someText) { return someText.replace ( /\n/gm, '<br />' ); }
Of course, that's just my opinion. I could be wrong.
-
Nov 6, 2003, 22:49 #5
- Join Date
- Mar 2002
- Posts
- 15
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ahh...that is better! Thanks, Mr. Brownstone.
-
Nov 7, 2003, 00:09 #6
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I use theses
Code:String.prototype.nl2br = function() { return this.replace( /\n/g, "<br />" ); }
Code:String.prototype.br2nl = function() { return this.replace( /\<br ?/?\>/g, "\n" ); }
-
Nov 7, 2003, 00:30 #7
- Join Date
- Mar 2003
- Location
- CA
- Posts
- 210
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
oh, you and your prototypes. when will you ever be satisfied.
-ChaCha
-
Nov 7, 2003, 00:50 #8
- Join Date
- May 2003
- Posts
- 1,843
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
...only when he owns the complete set
::: certified wild guess :::
-
Nov 7, 2003, 06:35 #9
-
Nov 7, 2003, 06:49 #10
Originally Posted by beetle
Bookmarks