0

In my attempt to divide a title into two lines, I fail in inserting therein a “carriage return” (or \r, or \n), to effectuate a two-line title.

I must add code before this line:

resultHdr = resultHdr + “Title B”;

I need to change the “resultHdr” earlier; It must already contain “Title A” - and a “carriage-return”, so that the line of code above would yield Title A and, on a next line, Title B.

It would seem absurdly simple to accomplish. But neither of these additions succeeded for me:?

resultHdr = "Title A” + "\n”;
resultHdr = “Title A \r”;
resultHdr = “Title A \n”;
resultHdr = resultHdr +  document.createElement('br');

I fail in breaking the output line into two; It always shows up as “Title ATitle B” - all on the same line!

The program involves DOM scripting and here may lie the reason for my “faulty” rendering.

(There's a lot of stuff written on this, like typing in \\n or replacing with regular expressions, but they are years old and obviously things ain't still all that cut and dry). (Meanwhile - I'm stuck!)

ruffy
  • 9
  • 6
  • Though this question may (and probably) has been asked before, I don't think the question that this was marked as a duplicate of is the same. – Tom Jul 06 '15 at 20:19
  • I agree with @Tom. Regardless what the other question says, browsers will collapse any and all whitespace (spaces, tabs, newlines, even explicit `\r` or `\n`) in the document to one space. Newlines should be replaced with `
    `. The other question's accepted answer is specific to tooltips and won't work anywhere else.
    – Corey Ogburn Jul 06 '15 at 20:24
  • You know how HTML treats _all_ white-space characters under normal circumstances, right? It condenses them to one single space character. If you want to change that, you either need to use special HTML elements that treat it differently (like `pre`), or format it using CSS to do the same (`white-space` property). A `br` element would be the right way to create a line break in “normal” HTML – but trying to concatenate the result of `document.createElement` to a string is of course nonsense. – CBroe Jul 06 '15 at 20:25
  • Maybe I'm still wet behind the ear, but I still cannot resolve my problem. Moving along my problem only exacerbated it because I had to try out another bunch of "try this" and "try that" non-solutions. Yet my quandary remains, only now really obscured from any help. – ruffy Jul 07 '15 at 05:40
  • Anyhow, thanks to CBroe's comment above, I discovered I need only embed my formatted string inside the PRE element and append IT with the contained string. Thanks again CBroe! – ruffy Jul 07 '15 at 18:04

0 Answers0