Add variable in html code

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 
 


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

Add variable in html code

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 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

This element only includes the global attributes.

Usage notes

Other elements that are used in contexts in which is commonly used include:

  • : The HTML Code element
  • : The HTML Keyboard input element
  • : The HTML Sample Output element

If you encounter code that is mistakenly using 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.

<p>A simple equation: <var>xvar> = <var>yvar> + 2p>

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

<p>
  The variables <var>minSpeedvar> and <var>maxSpeedvar> control the minimum
  and maximum speed of the apparatus in revolutions per minute (RPM).
p>

This HTML uses to enclose the names of two variables.

Result

Specifications

Specification
HTML Standard
# the-var-element

Browser compatibility

BCD tables only load in the browser

Does HTML support variables?

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.