Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spread html in multiple lines javascript

I want to put the below html code in my javascript function. I don't want to put it all next to each other. Is it possible to the code in the same way as how it is in html? Code:

    <div data-role="page" data-add-back-btn="true" id="Gallery2" class="gallery-page">

        <div data-role="header">
            <h1>Second Gallery</h1>
        </div>

        <div data-role="content">   

            <ul class="gallery">

                <li><a href="images/full/010.jpg" rel="external"><img src="images/thumb/010.jpg" alt="Image 010" /></a></li>
                <li><a href="images/full/011.jpg" rel="external"><img src="images/thumb/011.jpg" alt="Image 011" /></a></li>
                <li><a href="images/full/012.jpg" rel="external"><img src="images/thumb/012.jpg" alt="Image 012" /></a></li>
                <li><a href="images/full/013.jpg" rel="external"><img src="images/thumb/013.jpg" alt="Image 013" /></a></li>
                <li><a href="images/full/014.jpg" rel="external"><img src="images/thumb/014.jpg" alt="Image 014" /></a></li>
                <li><a href="images/full/015.jpg" rel="external"><img src="images/thumb/015.jpg" alt="Image 015" /></a></li>
                <li><a href="images/full/016.jpg" rel="external"><img src="images/thumb/016.jpg" alt="Image 016" /></a></li>
                <li><a href="images/full/017.jpg" rel="external"><img src="images/thumb/017.jpg" alt="Image 017" /></a></li>
                <li><a href="images/full/018.jpg" rel="external"><img src="images/thumb/018.jpg" alt="Image 018" /></a></li>

            </ul>

        </div>

        <div data-role="footer">
            <h4>&copy; 2011 Code Computerlove</h4>
        </div>

    </div>
like image 892
michael643 Avatar asked Nov 12 '11 11:11

michael643


People also ask

How do you write multiple lines in HTML?

To create a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. It is used within a form, to allow users to input text over multiple rows.

How do I split a string into multiple lines in HTML?

You can use the . split() method and the . join() method together to split a string into multiple lines.

How do you write multiple lines in JavaScript?

There are three ways to create a multiline string in JavaScript. We can use the concatenation operator, a new line character (\n), and template literals. Template literals were introduced in ES6. They also let you add the contents of a variable into a string.

How do you put multiple lines in a code?

To break a single statement into multiple lines The underscore must be immediately preceded by a space and immediately followed by a line terminator (carriage return) or (starting with version 16.0) a comment followed by a carriage return.


2 Answers

Updated, seems like Javascript does support multi lines by adding a backslash ( \ ) as the last character in the line, you can't even have a space bar behind it, because then it won't cancel the line break.

    var str = '<div data-role="page" data-add-back-btn="true" id="Gallery2" class="gallery-page"> \
    <div data-role="header">  \
        <h1>Second Gallery</h1> \
    </div> \
    <div data-role="content"> \  
        <ul class="gallery"> \
            <li><a href="images/full/010.jpg" rel="external"><img src="images/thumb/010.jpg" alt="Image 010" /></a></li> \
            <li><a href="images/full/011.jpg" rel="external"><img src="images/thumb/011.jpg" alt="Image 011" /></a></li> \
            <li><a href="images/full/012.jpg" rel="external"><img src="images/thumb/012.jpg" alt="Image 012" /></a></li> \
            <li><a href="images/full/013.jpg" rel="external"><img src="images/thumb/013.jpg" alt="Image 013" /></a></li> \
            <li><a href="images/full/014.jpg" rel="external"><img src="images/thumb/014.jpg" alt="Image 014" /></a></li> \
            <li><a href="images/full/015.jpg" rel="external"><img src="images/thumb/015.jpg" alt="Image 015" /></a></li> \
            <li><a href="images/full/016.jpg" rel="external"><img src="images/thumb/016.jpg" alt="Image 016" /></a></li> \
            <li><a href="images/full/017.jpg" rel="external"><img src="images/thumb/017.jpg" alt="Image 017" /></a></li> \
            <li><a href="images/full/018.jpg" rel="external"><img src="images/thumb/018.jpg" alt="Image 018" /></a></li> \
        </ul> \
    </div> \
    <div data-role="footer"> \
        <h4>&copy; 2011 Code Computerlove</h4> \
    </div> \
</div>';

Or use a HTML template and load it into a DIV (using jQuery)

$("#div").load("/html_template.html");
like image 154
Niels Avatar answered Oct 26 '22 10:10

Niels


Really old question, but I don't find this syntax a lot, so I'll just leave this here, as to me it is the most readable.

var html = '' +
    '    <div data-role="page" data-add-back-btn="true" id="Gallery2" class="gallery-page">' +
    '' +
    '        <div data-role="header">' +
    '            <h1>Second Gallery</h1>' +
    '        </div>' +
    '' +
    '        <div data-role="content">   ' +
    '' +
    '            <ul class="gallery">' +
    '' +
    '                <li><a href="images/full/010.jpg" rel="external"><img src="images/thumb/010.jpg" alt="Image 010" /></a></li>' +
    '                <li><a href="images/full/011.jpg" rel="external"><img src="images/thumb/011.jpg" alt="Image 011" /></a></li>' +
    '                <li><a href="images/full/012.jpg" rel="external"><img src="images/thumb/012.jpg" alt="Image 012" /></a></li>' +
    '                <li><a href="images/full/013.jpg" rel="external"><img src="images/thumb/013.jpg" alt="Image 013" /></a></li>' +
    '                <li><a href="images/full/014.jpg" rel="external"><img src="images/thumb/014.jpg" alt="Image 014" /></a></li>' +
    '                <li><a href="images/full/015.jpg" rel="external"><img src="images/thumb/015.jpg" alt="Image 015" /></a></li>' +
    '                <li><a href="images/full/016.jpg" rel="external"><img src="images/thumb/016.jpg" alt="Image 016" /></a></li>' +
    '                <li><a href="images/full/017.jpg" rel="external"><img src="images/thumb/017.jpg" alt="Image 017" /></a></li>' +
    '                <li><a href="images/full/018.jpg" rel="external"><img src="images/thumb/018.jpg" alt="Image 018" /></a></li>' +
    '' +
    '            </ul>' +
    '' +
    '        </div>' +
    '' +
    '        <div data-role="footer">' +
    '            <h4>&copy; 2011 Code Computerlove</h4>' +
    '        </div>' +
    '' +
    '    </div>';

With a simple shell command to generate this from an html file:

cat myfile.html | sed "s/'/\\'/g" | sed "$ ! s/^.*$/'\0' +/g" | sed "$ s/^.*$/'\0';/"

Update: With ES6, simply use backticks:

const html = `
  <div>
    <!-- Content -->
  </div>
`;
like image 41
Jerska Avatar answered Oct 26 '22 10:10

Jerska