HTML - <output> Tag



The HTML <output> tag is a flexible and under used component that enables programmers to dynamically show the outcomes of calculations or scripts inside the content. It improves the overall user experience by offering a quick and meaningful way to display the results of user activities or computations.

It can be used in forms, math-related material, or any other part of the document where dynamic output needs to be given because it is a self-closing tag, which means it doesn't need a closing tag.

Syntax

Following is the syntax for HTML <datalist> tag −

<datalist>.......</datalist>

Specific Attributes

The HTML <output > tag also supports the following additional attributes −

S.No. Attribute & Description
1

for

List of IDs of other elements, i.e it indicates the elements who have contributed input value to the calculation.
2

form

Enables to place output elements anywhere within a document.
3

name

It is the name of the element.

Example

Following is the example, where we are providing the range slider along with a inputfield and considering the sum of the user given values.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>HTML output tag</title>
</head>
<body>
   <!--create output tag-->
   <form oninput="add.value = parseInt(n1.value) + parseInt(n2.value)">
      <input type="range" min="0" max="100" name='n1' value="10">
      <input type="number" name='n2' value="20">
      <br> Output: <output name='add'></output>
   </form>
</body>
</html>

On running the above code, it will generate an output consisting of the range slider along with a input field displayed on the webpage.

Example

Considering the another scenario, where we are going to take two input fields with predefined values and retrieving the output as the combination of the both the values.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>HTML output tag</title>
</head>
<body>
   <!--create output tag-->
   <form oninput="add.value = (txt1.value).concat(txt2.value)">
      <input type="text" name='txt1' value="HTML">
      <input type="text" name='txt2' value="CSS">
      <br> Output: <output name='add'></output>
   </form>
</body>
</html>

When we run the above code, it will generate an output consisting of the two input field provided with a data displayed on the webpage.

Example

In the following example, we are going to perform the arthimetic operations.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>HTML output tag</title>
</head>
<body>
   <!--create output tag-->
   <form oninput="sum.value = parseInt(n1.value) + parseInt(n1.value) + parseInt(n3.value)">
      <input type="range" value="10" name='n1'> + <input type="number" value="5" name='n2'> + <input type="number" value="10" name='n3'> = <output name='sum'></output>
   </form>
</body>
</html>

On running the above code, it will generate an output consisting of the input fields that are going to perform the arithmetic operations and provide the output.

Example

Let's look at the following example, where we are going to apply CSS to the output tag.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>HTML output tag</title>
   <style>
      output {
         width: 200px;
         height: 50px;
         color: blue;
         padding: 10px;
         background-color: cadetblue;
         font-size: 20px;
      }
   </style>
</head>
<body>
   <!--create output tag-->
   <form oninput="sum.value = parseInt(n1.value) + parseInt(n1.value)">
      <input type="range" value="5" name='n1'> + <input type="number" value="2" name='n2'>
      <br>
      <br> Output: <output name='sum' for="n1 n2"></output>
   </form>
</body>
</html>

When we run the above code, it will generate an output consisting input field along with a CSS applied to the output tag displayed on the webpage.

html_tags_reference.htm
Advertisements