I am using String.fromCharCode to add new line (or) carriage return to my html text.
It is like,
"Ant the other line here..." + String.fromCharCode(13)
Jsfiddle is #http://jsfiddle.net/udmfsvfn/
But, no newline characters are added in the page!!
If you want a line break in HTML you need to supply <br> entity instead of a line break (your String.fromCharCode(13) code) since HTML interprets line breaks as usual spaces in text.
The reason that a newline character is not forcing a new line, is that in HTML a newline character doesnt do anything and is ignored.
In order to force text onto a new line, you will need to add the appropriate HTML tag, such as <BR> or put your text inside paragraphs <p>.
See the updated fiddle.
String.fromCharCode(10);
with CSS
white-space: pre-line;
Here si working example:
https://jsfiddle.net/Nxja/3xtcqdej/1/
Related
I have tag like <span style="font-size:10.5pt;\nfont-family:\nKaiTi"> and I want to replace \n within tag with empty character.
Note: Tag could be anything(not fixed)
I want regex expression to replace the same in the javascript.
You should be able to strip out the \n character before applying this HTML to the page.
Having said that, try this (\\n)
You can see it here: regex101
Edit: A bit of refinement and I have this (\W\\n). It works with the example you provided. It breaks down if you have spaces in the body of the tags (<span> \n </span>).
I've tried everything I know to do. Perhaps someone with more regex experience can assist?
I have the following JS string that creates markdown with a proper breakline between the two lines:
`this is some text${' '}
and this is a new line
`
Notice the two explicit spaces at the end of the first line. However, I can't figure out how to do two break lines. I've tried adding 4 spaces, but is not working.
What am I missing?
Not sure if this is exactly what you are looking for but you can throw in the line break <br /> character to let you skip as many lines as you like.
this is some text${'<br /><br />'}
this is some text${''}
You can use `` backticks for this: ES6 template strings.
let text = `this is some text${' '} \n \n and this is a new line`
You need to use a code block or raw HTML (see below). But, first let's address why your attempts do not work...
Markdown is not interpreted inside code blocks and spans. Otherwise, how would you demonstrate Markdown syntax within a Markdown document. Therefore, the double spaces to force a linebreak are ignored within your code span. Additionally, any <br> tag is assumed to be part of the code and escaped (as <br>) to be displayed in the span.
Code delimited by single backticks is a code span, not a code block. Code spans do not preserve all whitespace. They follow HTML's normal whitespace behavior of collapsing all whitespace to a single space.
If you want to preserve all whitespace, including newlines, then you should place your code in a code block. Within a code block, all newlines are preserved with no need to add extra spaces. There are a few ways to represent a code block. Of course, the standard way of indenting the block:
Some document text
this is some text${' '}
and this is a new line
More document text
Alternatively, if you are using a Markdown implementation which supports fenced code blocks (not all do), then you can used the triple backtick to delimit your block of code:
Some document text
```
this is some text${' '}
and this is a new line
```
More document text
Both of the above render the same:
Some document text
this is some text${' '}
and this is a new line
More document text
Notice that the newline is preserved. No special tricks necessary.
All of that said, if you really need a code span which contains a line break, that it outside of the scope of Markdown's syntax. However, as is always the case, anything you can't do in Markdown can be done with raw HTML. Therefore you could do the following:
<code>this is some text${' '}<br>and this is a new line</code>
Which renders as:
this is some text${' '}and this is a new line
I have text stored on Parse.com in the filed of type string.
The text consists of white-spaces such as end of line character/ new line
While displaying this text in HTML like paragraph.get('text') the new line characters are stripped.
How can I display the entire text with its formatting in HTML?
Option 1: Put your text between two pre tags:
<pre>
Text in a pre element
is displayed in a fixed-width
font, and it preserves
both spaces and
line breaks
</pre>
Options 2: use a regular expression (tons of examples out there) to replace your newline characters with something like a < br /> tag
I tried making a web page by writing
document.write("This is the first line \n And this is the second");
The desired effect is:
This is the first line
And this is the second
But I get this:
This is the first line And this is the second
I tried writing <br> inside and it worked. But Why writing an escape \n didn't? Anything I am doing wrong? I am using Firefox.
You are not doing anything wrong. Its just the \n character is valid in text related elements like, alert() and <textarea>.
The document.write() command writes to the body of the site or the display (in the HTML). Substitute <br> in place of the \n here since you are writing HTML in the page
instead of \n you have to use <br/>, so that the browser understands it
document.write("This is the first line <br/> And this is the second");
You use the <pre> tags for things like \n, and <br/> for ordinary HTML.
http://jsfiddle.net/FxpcA/
document.write("<pre>This is the first line \n And this is the second</pre>");
I really tried this a lot but I haven't got any result that actually worked.
I want to read the text of html textarea line by line using JQuery. Textarea has a fixed width and height. For example if the text area has the text like:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br/>
aaaaaaaaaaaaa just typed a real long<br/>
word that wont fit in side of this part of<br/>
div
I would like to have a string as
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa< br /> aaaaaaaaaaaaa just typed a real long< br />
word that wont fit in side of this part of <br />div
I've tried it and it did not work out. Any suggestions?
You can use JavaScript's split function on the newline character \n. Then, call join on the resulting array.
Working example: http://jsfiddle.net/XjN72/1/
You can just remove the newlines:
text = text.replace(/\n|\r/g, "");