4

I have a chunk of HTML which I'd like to add to a document at run time.

How do I embed the HTML within the JS file?

var p = document.createElement("div");
p.innerHTML = '<div>  MY HTML  
               <some element>  </some element>
               <some element>  </some element>
               </div>'

The problem is that my HTML is multiline; how do I add the newline chars within a JS string?

3
  • You don't want the <br/> tags do you? Commented Apr 30, 2009 at 17:11
  • 4
    Why do you care if your HTML is multiline? The browser will be stripping out the whitespace anyway. Commented Apr 30, 2009 at 17:12
  • 3
    @Thomas: Ever tried editing a large chunk of HTML (or anything else for that matter) that is place all on one line. Commented Apr 30, 2009 at 17:19

1 Answer 1

2

you can use backslash character to ignore end of line in JavaScript strings.

http://www.componentart.com/BLOGS/jovan/archive/2007/10/17/multiline-strings-in-javascript.aspx

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

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.