1

I'd like to display some well formatted HTML on my page. That HTML will be compiled in the script. To show that HTML I am using < pre ><code> HTML CONTENT </code>< /pre >

The problem is I have no Idea how to add line breaks and other formatting to the variable that is compiling the HTML.

MY SCRIPT:

var CodeShow = '<div id="builder" class="' + LoginClasses + '">
                <form id="LoginForm" class="formContainer' + FormClasses + '">
                </form></div>';
$('#CodeShow').text(CodeShow);

Expected HTML Result:

 <code>
    <div id="builder" class="classA">
          <form id="LoginForm" class="formContainer classB"></form>
    </div>
 </code>

This is a sample HTML however the real HTML has number of lines.

3
  • I think you need to use \n in your variable Commented Dec 20, 2017 at 11:14
  • Demo : jsfiddle.net/9zz9dy5L Commented Dec 20, 2017 at 11:20
  • 1
    @The_Death_Raw your code works but the output is going too wide since there are number of classes assigned to few elements. even used white-space: pre-line on the <code> but didn't work :( Also, please reply as an answer so that I am mark it. Thanks Commented Dec 20, 2017 at 11:49

2 Answers 2

1

var LoginClasses = " classA", FormClasses = " classB";
var CodeShow = '<div id="builder" class="' + LoginClasses + '"> \n <form id="LoginForm" class="formContainer' + FormClasses + '"> \n </form> \n </div>';
$(function() {
  $('#Code').html(CodeShow);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="#CodeShow"><code id="#Code"></code></pre>

Sign up to request clarification or add additional context in comments.

Comments

0

When I saw you using .text() I assumed you want to display the result on your webpage. Not just to take effect in your browser. Anyway to display this on the webpage.

var LoginClasses = " classA", FormClasses = " classB";

var CodeShow = `&lt;div id="builder" class= "${LoginClasses}"&gt;
   &lt;form id = "LoginForm" class ="formContainer ${FormClasses}"&gt;

   &lt;/form&gt;
&lt/div&gt`;

$(function(){
    $('#CodeShow code').html(CodeShow);
})

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.