Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a variable in jquery with html content

Tags:

html

jquery

var

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!

like image 988
luv2code Avatar asked Sep 08 '12 17:09

luv2code


People also ask

How do you assign a block of HTML code to a jQuery variable?

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.

How do you create a variable in HTML?

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.

How HTML elements are used in jQuery with example?

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.

What is HTML () in jQuery?

jQuery html() Method The html() method sets or returns the content (innerHTML) of the selected elements.


2 Answers

Syntax error due to wrongly assigned string.

concatenate the lines

var copy = "<table width='750' border='0' cellspacing='0' cellpadding='0'>" 
            + "<tr>";
  ....
like image 142
Adil Avatar answered Sep 22 '22 17:09

Adil


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>";
like image 34
dfsq Avatar answered Sep 24 '22 17:09

dfsq