<output> Tag in HTML
The <output> tag in HTML is used to represent the result of a calculation or user action. It is commonly used in forms, where it displays the outcome of an input or operation.
Key Points on <output> Tag:
- The <output> tag is used to show the result of a calculation or user action, such as after submitting a form.
- It is often used alongside form elements like input or select, where it updates dynamically based on the user’s input.
- The content of the <output> tag can be manipulated using JavaScript to show the result of calculations or events.
- The <output> tag is often used in combination with other elements such as the <form> or <input> tags to display results.
- It is an inline element, meaning it behaves like a typical inline tag in HTML, but it can also contain text or other HTML elements.
Syntax of <output> Tag:
Syntax Example
<output name="result"></output>
Example of <output> Tag in HTML:
This example shows how the <output>
tag is used to display the result of a simple calculation, such as adding two numbers.
Code Example
<form>
<label for="num1">Number 1:</label>
<input type="number" id="num1" value="10"><br>
<label for="num2">Number 2:</label>
<input type="number" id="num2" value="20"><br>
<button type="button" onclick="calculateResult()">Add Numbers</button><br>
<output name="result" id="result">0</output>
</form>
<script>
function calculateResult() {
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;
var result = parseInt(num1) + parseInt(num2);
document.getElementById('result').value = result;
}
</script>
Output
0
The <output>
tag is a useful HTML element for displaying the result of calculations or user actions in a form. It makes it easy to show real-time feedback based on user input, making it an essential tool for interactive web applications.