Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating HTML Content In a Variable in Javascript

I have a javascript variable I need to create like this:

var HTMLContent = '<div class="className">HTML Content</div>';

How can I format it in an easier to read format because I'm going to want to create multiple lines of HTML.

e.g.

var HTMLContent = '
<div class="className">
  HTML Content
</div>
';

Is something like that possible?

It would also be good if I could import via URL e.g. var HTMLContent = 'http://domain.com/page.html';

like image 487
Jordash Avatar asked Aug 04 '11 03:08

Jordash


1 Answers

 var longStr = "You can split\
 the string onto multiple lines\
 like so";

An example using your HTML would be:

var longStr = 
    '<div class="className">\
        HTML Content\
    </div>';

To load external HTML, check out jQuery's load method:

$('#result').load('ajax/test.html');
like image 192
Derek Hunziker Avatar answered Sep 28 '22 05:09

Derek Hunziker