Create a variable in jquery with html content

Hi, I am trying to create a variable in jquery that contains a table for output in different areas of a website. But that gives me an error, and I don’t understand why. Here is my jquery:
var copy = "<table width='750' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td>Tarifa valida desde:</td>
    <td>Tarifa valida hasta:</td>
    <td>Tarifa MXN</td>
    <td>Tarifa USD</td>
  </tr>
  <tr>
    <td><input type='text' name='from1' id='from1' class='date' /></td>
    <td><input type='text' name='to1' id='to1' class='date' /></td>
    <td><input type='text' name='mxn1' /></td>
    <td><input type='text' name='usd1' /></td>
  </tr>
  <tr>
    <td>Extra Pax MXN:</td>
    <td>Extra Pax USD:</td>
  </tr>
  <tr>
    <td><input type='text' name='exmxn1' /></td>
    <td><input type='text' name='exusd1' /></td>
  </tr>
</table>";
    });

How can I put this in a variable so that I can output to different divs like this:

$(".divExample").html(copy);

Thank you in advance for anyones help!

+5
source share
5 answers

Syntax error due to incorrectly assigned string.

concatenate strings

var copy = "<table width='750' border='0' cellspacing='0' cellpadding='0'>" 
            + "<tr>";
  ....
+3
source

You did not process the lines in the line. Because of this, javascript assumes that the end of each line is the end of the statement. Obviously, each line is not a valid expression. Align your line like this:

var "multi-"+
    "line "+
    "string";
+3
source

, . :

var html = "<table> \
    <tr>....</tr> \
    </table>";
+2

html, , . html DIV html-

var copy = $('#mycomplexhtml').html(); //gets the content I placed in an hidden div


<!-- I normally place this at the bottom-most part of the page -->
<div id="mycomplexhtml" style="display:none">
  <table width='750' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td>Tarifa valida desde:</td>
    <td>Tarifa valida hasta:</td>
    <td>Tarifa MXN</td>
    <td>Tarifa USD</td>
  </tr>
 ...
  </table>
</div>
+2
source

All Articles