473,395 Members | 1,938 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,395 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 9507
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.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.