473,226 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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

Similar topics

3
by: kenneth | last post by:
Hi, I'd like to know whether there's a php function that transform a string into html... espacially turning \n to <br> etc. I couldn't find it in the php doc. thx in advance, -- kenneth
2
by: Indrid Cold | last post by:
Is there a way to call a function so the function shares the scope of the caller? For example, I was playing around with making a php version of the cold fusion tag CFPARAM What that cold...
3
by: Matthew Sims | last post by:
Hey all, So I started using nl2br and love it. But when I convert back from <br> to \n, it produces double spaces instead of single space. Converting \n to <br>: $emailBody=nl2br($_POST);...
6
by: leegold2 | last post by:
I have been looking for a php word wrap function. I know there's an official PHP function but I've tried that and many of the functions contributed on that php.net page and none do what I...
11
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
2
by: Chris Schinzel | last post by:
Hi, I'm reading text from a html textarea field (standard wrap functionality, no value specified). If I display it via nl2br() (after html form submission), everything's ok. But if I send it via...
4
by: comp.lang.tcl | last post by:
I wrote this PHP function in the hopes that it would properly use a TCL proc I wrote about 4 years ago: if (!function_exists('proper_case')) { /** * Ths function will convert a string into a...
4
by: Nate12o6 | last post by:
Mabee you guys can help me with this. I have a form with enctype="multipart/form-data" that has a textarea in it as well as file upload. The text area is for a description of the file. If...
14
by: Jeigh | last post by:
Hello, I've created a form where users of my site can contact each other, when I'm processing the data I use: $message = nl2br(htmlspecialchars($_POST)); For example.
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.