Reputation: 3323
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?
Upvotes: 9
Views: 14772
Reputation: 23873
This question had me curious, so I had to check the source: http://www.w3.org/TR/html401/struct/global.html#adef-title
The W3C is completely silent on how various form of white space within an attribute is treated, so I am force to assume it follows the same rules as HTML.
Under HTML, unless under a <PRE>
element or similar CSS
rule, all white space is collapsed to a single space for rendering.
Thus:
<p>The quick brown
fox
jumped
over the
lazy dog</p>
is rendered the same as:
<p>The quick brown fox jumped over the lazy dog</p>
This HTML behavior, combined with fact that user agents (browsers) may render the TITLE
attribute in any way they wish, makes it likely that most (if not all) such agents would have no reason to preserve end-of-line markers (carriage return
and newline
or \r\n
).
Edit:
If the formatting is important, the title
text could be escaped and put inside a data-*
element. Using a mouse-over, a custom popup box could show the formatted HTML instead.
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 read more about data-*
attributes here: http://www.javascriptkit.com/dhtmltutors/customattributes.shtml
In short, in some mouseover
code, you can find the text with:
var mytitle = <element>.getAttribute("data-title");
Then use a popup div to display mytitle
Upvotes: 0
Reputation: 431
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
Upvotes: 18
Reputation: 13568
According to the HTML specification all line breaks should be "escaped" aka replayed by 

http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-line-ends
http://www.w3.org/TR/2006/REC-xml11-20060816/#AVNormalize
Upvotes: 0
Reputation: 2377
you can use javascript global methods escape()
to encode your string v and unescape() to decode it back for use.
here is your5 fiddle
in your code this line can be like this
var v = escape($('#'+i).parent().attr('title'));
Upvotes: 2
Reputation: 2613
You would need to read this from the database directly. The line endings are being interpreted when the HTML is being generated for the site which is why you see the multi-line td element in the code.
When you go to reference that td element, it spits out the value in a single line. Once this data is lost, you can't recreate it in any manner.
Upvotes: 0
Reputation: 1376
Well, if you put the string into a text input box, there will be no new lines since the text input box is for single-line only text.
The \n
are there - you can try splitting by them:
lines = str.split("\n")
Upvotes: 1