Hi I am trying to create a variable in jquery that contains a table for outputting in different areas of a website. But it is giving me an error, and I do not 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 could I place this in a variable so that I can output in various divs as so:
$(".divExample").html(copy);
Thank you in advance for anyones help!
Answer: Use the concatenation operator (+) The simple and safest way to use the concatenation operator ( + ) to assign or store a bock of HTML code in a JavaScript variable. You should use the single-quotes while stingify the HTML code block, it would make easier to preserve the double-quotes in the actual HTML code.
Use the <var> tag in HTML to add a variable. The HTML <var> tag is used to format text in a document. It can include a variable in a mathematical expression.
What is the use of html() method in jQuery ? The html() method in jQuery is used to get the contents of the first element in the set of matched elements or is used to set the HTML contents of every matched element. It returns the content of the first matched element. This function does not accept any arguments.
jQuery html() Method The html() method sets or returns the content (innerHTML) of the selected elements.
Syntax error due to wrongly assigned string.
concatenate the lines
var copy = "<table width='750' border='0' cellspacing='0' cellpadding='0'>"
+ "<tr>";
....
You could concatenate strings like it was suggested. Or another way is to escape new line characters with back slash:
var html = "<table> \
<tr>....</tr> \
</table>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With