HTML for Attribute

The for attribute of the <output> element establishes a relationship between the calculation result and the input elements used in that calculation. This attribute helps browsers and assistive technologies understand which form controls contribute to the output value.

Syntax

Following is the syntax for the for attribute −

<output for="id1 id2 id3">result</output>

The value is a space-separated list of element IDs that participate in the calculation. These IDs must reference form controls within the same document.

Parameters

The for attribute accepts the following parameter −

  • id list − A space-separated string of element IDs that contribute to the output calculation. Each ID must correspond to a form control element like <input>, <select>, or <textarea>.

Basic Calculator Example

Following example demonstrates the for attribute with a simple addition calculator −

<!DOCTYPE html>
<html>
<head>
   <title>Output For Attribute Example</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Simple Calculator</h2>
   <form oninput="result.value=parseInt(num1.value)+parseInt(num2.value)">
      <label for="num1">First Number:</label>
      <input type="number" id="num1" name="num1" value="10" min="0" max="100">
      <br><br>
      
      <label for="num2">Second Number:</label>
      <input type="number" id="num2" name="num2" value="20" min="0" max="100">
      <br><br>
      
      <label>Result: </label>
      <output name="result" for="num1 num2">30</output>
   </form>
</body>
</html>

The output displays an interactive calculator where changing either input updates the result automatically −

Simple Calculator

First Number: [10]

Second Number: [20]

Result: 30

Range Slider with Multiple Inputs

Following example shows the for attribute with a range slider and number inputs −

<!DOCTYPE html>
<html>
<head>
   <title>Range Slider Calculator</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Multi-Input Calculator</h2>
   <form oninput="total.value=parseInt(slider.value)+parseInt(input1.value)+parseInt(input2.value)">
      <label for="slider">Slider (0-100):</label>
      <input type="range" id="slider" name="slider" value="25" min="0" max="100">
      <span id="sliderValue">25</span>
      <br><br>
      
      <label for="input1">Number 1:</label>
      <input type="number" id="input1" name="input1" value="30" min="0">
      <br><br>
      
      <label for="input2">Number 2:</label>
      <input type="number" id="input2" name="input2" value="45" min="0">
      <br><br>
      
      <strong>Total Sum: <output name="total" for="slider input1 input2">100</output></strong>
   </form>
   
   <script>
      document.getElementById('slider').oninput = function() {
         document.getElementById('sliderValue').textContent = this.value;
      }
   </script>
</body>
</html>

This example creates a calculator that sums values from a range slider and two number inputs. The for="slider input1 input2" attribute indicates all three controls contribute to the total −

Multi-Input Calculator

Slider (0-100): [====|    ] 25

Number 1: [30]

Number 2: [45]

Total Sum: 100

Percentage Calculator

Following example demonstrates calculating percentage using the for attribute −

<!DOCTYPE html>
<html>
<head>
   <title>Percentage Calculator</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Grade Percentage Calculator</h2>
   <form oninput="percentage.value=Math.round((obtained.value/total.value)*100) || 0">
      <label for="obtained">Marks Obtained:</label>
      <input type="number" id="obtained" name="obtained" value="85" min="0">
      <br><br>
      
      <label for="total">Total Marks:</label>
      <input type="number" id="total" name="total" value="100" min="1">
      <br><br>
      
      <label>Percentage: </label>
      <output name="percentage" for="obtained total">85</output>%
   </form>
</body>
</html>

The calculator shows the percentage based on obtained marks and total marks. The for attribute links both input fields to the percentage output −

Grade Percentage Calculator

Marks Obtained: [85]

Total Marks: [100]

Percentage: 85%
How the For Attribute Works Input A Input B Output for="inputA inputB" The for attribute creates semantic relationship between inputs and output

Key Points

  • The for attribute is optional but recommended for accessibility and semantic clarity.
  • IDs referenced in the for attribute must exist in the same document.
  • Multiple IDs are separated by spaces in the for attribute value.
  • The for attribute does not affect the visual appearance or calculation logic − it only establishes semantic relationships.
  • Screen readers and other assistive technologies use this information to better understand form relationships.

Browser Compatibility

The for attribute of the <output> element is supported in HTML5-compliant browsers including Chrome, Firefox, Safari, Edge, and Opera. Internet Explorer does not support the <output> element.

Conclusion

The for attribute of the <output> element creates a semantic relationship between calculation results and their input sources. While it doesn't affect functionality, it improves accessibility and helps assistive technologies understand form relationships. Always include relevant input element IDs in the for attribute value when using <output> elements.

Updated on: 2026-03-16T21:38:53+05:30

179 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements