How to keep a line break

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

Enter this code on the page (which comes from a 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, line breaks are lost and use the variable v in the text input field:

 This from RicardAnd also a testRent 3000.00 

What does it take to save \n's and look like the one below?

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

+6
source share
6 answers

Line breaks still exist, and the browser is simply trying to display them.

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

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

Otherwise, if you want to NOT see "\ n" and instead see line breaks, you can do it like this:

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

Check JSBin

+13
source

you can use javascript escape() global methods to encode your v string and unescape () to decode it for use.

here is your script

http://jsfiddle.net/jYNKG/

in your code this line may be like this

 var v = escape($('#'+i).parent().attr('title')); 
+2
source

Well, if you put a string in a text box , there will be no new lines, since the text input field is intended for text for only one line.

There is \n - you can try to break them:

 lines = str.split("\n") 
+1
source

I am curious about this question, so I had to check the source: http://www.w3.org/TR/html401/struct/global.html#adef-title

W3C was completely silent about how the various forms of whitespace are handled inside an attribute, so I can assume that it follows the same rules as HTML.

In HTML, if only in a <PRE> element or similar CSS rule, all spaces are collapsed into a single rendering space.

Thus:

 <p>The quick brown fox jumped over the lazy dog</p> 

displayed the same as:

 <p>The quick brown fox jumped over the lazy dog</p> 

This HTML behavior, combined with the fact that user agents (browsers) can render the TITLE attribute as they see fit, makes it likely that most (if not all) of these agents have no reason to save the end ( carriage return and newline or \r\n ).

Edit:

If formatting is important, TITLE text can be escaped and placed inside the data-* element. Using a mouse, a custom popup can display formatted HTML.

Edit 2 - taken from your original question:

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

You can learn more about data-* attributes here: http://www.javascriptkit.com/dhtmltutors/customattributes.shtml

In short, in mouseover code you can find text with:

 var mytitle = <element>.getAttribute("data-title"); 

Then use the popup to display mytitle

0
source

You will need to read this directly from the database. Line endings are interpreted when HTML is created for the site, so you see a multi-line td element in the code.

When you go to the link to this td element, it splashes out the value in one line. Once this data is lost, you will not be able to recreate it in any way.

0
source

According to the HTML specification, all line breaks must be "escaped" and also played using &#xA;

http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-line-ends

http://www.w3.org/TR/2006/REC-xml11-20060816/#AVNormalize

0
source

Source: https://habr.com/ru/post/925485/


All Articles