Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What HTML semantics should I use to make the output of a custom calculator accessible?

I’m developing a simple calculator tool. Right now, I’m using an <input> element with the readonly attribute to style the result of the calculation. However, while I think that this produces desirable aesthetics, I’m concerned that this is a poor choice for accessibility, given that this is not an input, it’s an output. Is a different HTML Element more appropriate here?

Here is a small reproduction of what I’m trying to do:

<h1>Triangle Area Calculator</h1>
<form>
    <ul>
        <li>
            <label for="base">Triangle base:</label>
            <input type="number" step="any" id="base" name="base" required onchange="checkComplete()">
        </li>
        <li>
            <label for="height">Triangle height:</label>
            <input type="number" step="any" id="height" name="height" required onchange="checkComplete()">
        </li>
        <li>
            <label for="area">Triangle area:</label>
            <input type="number" step="any" class="result" id="area" name="result" readonly>
        </li>
    </ul>
</form>
form ul {
  list-style: none;
}
input.result{
  background-color: whitesmoke;
}
function checkComplete() {
    base_input = document.getElementById("base")
  height_input = document.getElementById("height")
  area_result = document.getElementById("area")
  
  if (base_input.checkValidity() && height_input.checkValidity()){
    area_result.value = base_input.value * height_input.value/2
  }
  else{
    area_result.value = ""
  }
}

Here is the above in a JSFiddle project.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

<output>: The Output element

"use strict";

function checkComplete() {
  let base_input = document.getElementById("base");
  let height_input = document.getElementById("height");
  let area_result = document.getElementById("area");

  if (base_input.checkValidity() && height_input.checkValidity()) {
    area_result.value = base_input.value * height_input.value / 2;
  } else {
    area_result.value = "";
  }
}
form ul {
  list-style: none;
}

input.result {
  background-color: whitesmoke;
}
<h1>Triangle Area Calculator</h1>
<form>
  <ul>
    <li>
      <label for="base">Triangle base:</label>
      <input type="number" step="any" id="base" name="base" required onchange="checkComplete()">
    </li>
    <li>
      <label for="height">Triangle height:</label>
      <input type="number" step="any" id="height" name="height" required onchange="checkComplete()">
    </li>
    <li>
      <label for="area">Triangle area:</label>
      <output for="base height" class="result" id="area" name="result"></output>
    </li>
  </ul>
</form>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading