Yarin
Yarin

Reputation: 183569

Handling line-breaks across XML/HTML/JQuery

I'm loading an XML file with ajax, dynamically writing/reading values to/from HTML form textareas using jQuery, and then saving back to an XML file by passing it to a PHP-write function on a form submit.

What I'm looking for is a consistent way to handle line-breaks and paragraphs across HTML/jQuery/XML- Something where users would see actual line-breaks, not code, in html and form fields.

Up until now I've been using <br/> tags in CData fields in XML, but these show up in form fields, and jQuery is making a mess of my CData writes.

Any good ideas? Thanks-

Upvotes: 2

Views: 1327

Answers (3)

johnny craig
johnny craig

Reputation: 13

unless i misunderstand the question, php has the built-in functions. nl2br($string); and br2nl($string); you could save the data with nl2br and then load it normally everywhere except the text areas. load to the text area using br2nl($string);

Upvotes: 1

Jason Gennaro
Jason Gennaro

Reputation: 34855

You could use some regex to switch out the <br /> for \n and \r when writing to the form fields.

Upvotes: 0

Jacob Mattison
Jacob Mattison

Reputation: 51052

If you use actual return characters in the text, then in order to make them show up when displaying as HTML, you could set the css property white-space:pre or white-space:pre-line.

Upvotes: 3

Related Questions