2

a JavaScript n00b here...
I'm generating some html code in javascript, that is going to be displayed as code via the prism HTML markup plugin. The code is dynamically added to a <pre> tag on a button click.

My javascript code is as below. It is the text in line 2, where I need a line break. I have tried /n but that doesn't work it just makes a space.

var startLabelTag = document.createTextNode("text goes here");
startLabelTag.nodeValue = "<label><strong>" + elementNameFinal + "</strong></label>LINEBREAK HERE<select id='dropdownmenu' class='Custom_" + fieldNameFinal + "' onchange='selectChanged('@field[" + fieldNameFinal + "]',this.value);'>";
document.getElementById("dropdown-code").appendChild(startLabelTag);

Below is the text string I'm trying to create, where a line break is made where the text LINEBREAK HERE is.

<label><strong>" + elementNameFinal + "</strong></label>LINEBREAK HERE<select id='dropdownmenu' class='Custom_" + fieldNameFinal + "' onchange='selectChanged('@field[" + fieldNameFinal + "]',this.value);'>
6
  • Would a normal HTML line break work here? <br/> Commented Feb 1, 2016 at 12:50
  • No because the HTML is shown as markup, so it would just be shown as "plain text" Commented Feb 1, 2016 at 12:53
  • Ah yes I see. Hmmm.... Commented Feb 1, 2016 at 12:54
  • have you tried \r\n ? Commented Feb 1, 2016 at 12:55
  • Also only makes a space Commented Feb 1, 2016 at 12:59

2 Answers 2

1

Is it something like this you are looking for?

By using String.fromCharCode(10) you can insert a line break and with the pre tag (or div having white-space: pre-wrap) the line break will be visible/shown.

var elementNameFinal = "elementname", fieldNameFinal = "fieldname";

var startLabelTag = document.createTextNode("text goes here");
startLabelTag.nodeValue = "<label><strong>" + elementNameFinal + "</strong></label>" + String.fromCharCode(10) + "<select id='dropdownmenu' class='Custom_" + fieldNameFinal + "' onchange='selectChanged('@field[" + fieldNameFinal + "]',this.value);'>";

document.getElementById("dropdown-code").appendChild(startLabelTag);
<pre id="dropdown-code"></pre>

Side note

You can of course use a div as well, having the CSS rule Niet the Dark Absol suggested.

<div id="dropdown-code"></div>

#dropdown-code {
  white-space: pre-wrap;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Add white-space: pre-wrap to the container's CSS.

After all, if you type a newline in your HTML source, do you get a blank line in the result? Nope. Not without making whitespace significant through CSS.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.