Topic: JavaScript / jQueryPrev|Next
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.
You also need to escape the single-quotes which appears inside the content of HTML block — just replace the '
character with \'
, as shown in the following example:
Store HTML Code in JavaScript Variable
// Storing HTML code block in a variable
var codeBlock = '' +
'This is a heading
' +
'This is a paragraph of text.
' +
'Note: If you don\'t escape "quotes" properly, it will not work.
' +
'';
// Inserting the code block to wrapper element
document.getElementById["wrapper"].innerHTML = codeBlock
Related FAQ
Here are some more FAQ related to this topic:
- How to add elements to DOM in jQuery
- How to remove wrapper element but keep the text content intact using jQuery
- How to bind click event to dynamically added HTML elements in jQuery
The
element in HTML defines a text as a variable and can be used to define variables of a mathematical expression or variables of a piece of code. The text enclosed in the
element is normally displayed in italics.
Note: The
element supports all global attributes and event attributes.
Syntax
Example
The following example demonstrates how to use the element in HTML.
The example above shows the code to add two numbers written in HTML. Each of the variables appears italicized because they are enclosed in a tag. The
tag is added for formatting.
The This element only includes the global
attributes. Other elements that are used in contexts in which If you encounter code that is mistakenly using HTML element represents the name of a variable in a mathematical expression or a programming context. It's typically presented using an italicized version of the current typeface, although that behavior is browser-dependent.
Try it
Attributes
Usage notes
is commonly used include:
:
The HTML Code element
: The HTML Keyboard input element
: The HTML Sample Output element
for style purposes rather than semantic purposes, you should either use a
with appropriate CSS or, an appropriate semantic element among the following:
Default style
Most browsers apply font-style
to "italic"
when
rendering . This can be overridden in CSS, like this:
var {
font-style: normal;
}
Examples
Basic example
Here's a simple example, using to denote variable names in a mathematical equation.
A simple equation: x = y + 2
The output:
Overriding the default style
Using
CSS, you can override the default style for the element. In this example, variable names are rendered using bold Courier if it's available, otherwise it falls back to the default monospace font.
CSS
var {
font: bold 15px "Courier", "Courier New", monospace;
}
HTML
The variables minSpeed and maxSpeed control the minimum
and maximum speed of the apparatus in revolutions per minute [RPM].
This HTML uses to enclose the names of two variables.
Result
Specifications
HTML Standard # the-var-element |
Browser compatibility
BCD tables only load in the browser