Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve a line break

Line breaks and carriage returns... Must be the most complicated part of coding. (for me)

Have this code in a page (which came from database stored as This from Ricardo\nAnd also a test\nRent 3000.00):

<td title="This from Ricard
And also a test
Rent 3000.00" style="width:198px;text-align:left">....</td>

Then use this to get the title attribute with 'hidden' \n's

var v = $('#'+i).parent().attr('title');        // i = id of <span> child of <td>

Along the way, the line breaks get lost and using the variable v into a text input box:

This from RicardAnd also a testRent 3000.00

What does it take to preserve the \n's and have this look instead like below?

Screenshot of desired result (with \n's present) after clicking edit

like image 984
David Avatar asked Sep 15 '12 22:09

David


1 Answers

The line breaks are still there, it is just that the browser tries to render them.

If you still want to see the "\n" try this:

var v = $('td:first-of-type').attr('title').replace(/\n/g, "\\n");

Else, if what you want is NOT to see the "\n" and instead see the line breaks, you could do it this way:

var v = $('td:first-of-type').attr('title').replace(/\n/g, "<br/>");

Check this JSBin

like image 127
ngonzalvez Avatar answered Sep 28 '22 09:09

ngonzalvez