How do I delete document.write() out of my calculator

Viewed 69

How do I make this calculator display the result on the first page after the ='s sign without destroying all of the html on the page with document.write()?

I know that document.write() is the problem, but I don't know of anything else to use. I'm very new to coding, so any help is greatly appreciated.

I also have a problem with the division part because it is putting the result right next to the remainder, however, once the document.write() problem is resolved, I think that the solution should become more apparent. Thank You!

<head>
  <script language="javascript" type="text/javascript">
    function add() {
      var input1 = parseInt(document.getElementById("t1").value);
      var input2 = parseInt(document.getElementById("t2").value);
      var result = input1 + input2;
      document.write(result);
    }

    function divide() {
      var input1 = parseInt(document.getElementById("t3").value);
      var input2 = parseInt(document.getElementById("t4").value);
      var result = Math.floor(input1 / input2);
      var remainder = input1 % input2
      document.write(result)
      document.write(remainder)
    }

    function multiply() {
      var input1 = parseInt(document.getElementById("t5").value);
      var input2 = parseInt(document.getElementById("t6").value);
      var result = input1 * input2;
      document.write(result);
    }

    function subtract() {
      var input1 = parseInt(document.getElementById("t7").value);
      var input2 = parseInt(document.getElementById("t8").value);
      var result = input1 - input2;
      document.write(result);
    }
  </script>
  <title>java</title>
</head>

<body>
  Addition
  <p>
    <input type="text" id="t1" name="t1"> +
    <input type="text" id="t2" name="t2">
    <input type="button" id="add" value="=" onClick="add();">
  </p>
  <p>
    Subtraction
    <p>
      <input type="text" id="t7" name="t7"> -
      <input type="text" id="t8" name="t8">
      <input type="button" id="subtract" value="=" onClick="subtract();">

      <p>Multiplication
        <p>
          <input type="text" id="t5" name="t5"> *
          <input type="text" id="t6" name="t6">
          <input type="button" id="multiply" value="=" onClick="multiply();">
        </p>
        <p>Division
          <p>
            <input type="text" id="t3" name="t3"> ÷
            <input type="text" id="t4" name="t4">
            <input type="button" id="divide" value="=" onClick="divide();">
          </p>


</body>

</html>

2 Answers

You can either use textContent or innerHTML.

Here's an example using textContent:

function add() {
  var input1 = parseInt(document.getElementById("t1").value);
  var input2 = parseInt(document.getElementById("t2").value);
  var result = input1 + input2;
  document.getElementById('add-result').textContent = result;
}

function divide() {
  var input1 = parseInt(document.getElementById("t3").value);
  var input2 = parseInt(document.getElementById("t4").value);
  var result = Math.floor(input1 / input2);
  var remainder = input1 % input2
  document.getElementById('divide-result').textContent = result;
  document.getElementById('divide-remainder').textContent = remainder;
}

function multiply() {
  var input1 = parseInt(document.getElementById("t5").value);
  var input2 = parseInt(document.getElementById("t6").value);
  var result = input1 * input2;
  document.getElementById('multiply-result').textContent = result;
}

function subtract() {
  var input1 = parseInt(document.getElementById("t7").value);
  var input2 = parseInt(document.getElementById("t8").value);
  var result = input1 - input2;
  document.getElementById('subtract-result').textContent = result;
}
<div>
  <h1>Addition</h1>
  <input type="text" id="t1" name="t1"> +
  <input type="text" id="t2" name="t2">
  <input type="button" id="add" value="=" onClick="add();">
  <span id="add-result"></span>
</div>
<div>
  <h1>Subtraction</h1>
  <input type="text" id="t7" name="t7"> -
  <input type="text" id="t8" name="t8">
  <input type="button" id="subtract" value="=" onClick="subtract();">
  <span id="subtract-result"></span>
</div>
<div>
  <h1>Multiplication</h1>
  <input type="text" id="t5" name="t5"> *
  <input type="text" id="t6" name="t6">
  <input type="button" id="multiply" value="=" onClick="multiply();">
  <span id="multiply-result"></span>
</div>
<div>
  <h1>Division</h1>
  <input type="text" id="t3" name="t3"> ÷
  <input type="text" id="t4" name="t4">
  <input type="button" id="divide" value="=" onClick="divide();">
  <span id="divide-result"></span> | 
  <span id="divide-remainder"></span>
</div>


With textContent you can only set text, with innerHTML you can set HTML:

function add() {
  var input1 = parseInt(document.getElementById("t1").value);
  var input2 = parseInt(document.getElementById("t2").value);
  var result = input1 + input2;
  document.getElementById('add-result').innerHTML = `<i style="color: blue">${result}</i>`;
}

function divide() {
  var input1 = parseInt(document.getElementById("t3").value);
  var input2 = parseInt(document.getElementById("t4").value);
  var result = Math.floor(input1 / input2);
  var remainder = input1 % input2
  document.getElementById('divide-result').innerHTML = `<i style="color: blue">${result}</i>`;
  document.getElementById('divide-remainder').innerHTML = `<i style="color: blue">${remainder}</i>`;
}

function multiply() {
  var input1 = parseInt(document.getElementById("t5").value);
  var input2 = parseInt(document.getElementById("t6").value);
  var result = input1 * input2;
  document.getElementById('multiply-result').innerHTML = `<i style="color: blue">${result}</i>`;
}

function subtract() {
  var input1 = parseInt(document.getElementById("t7").value);
  var input2 = parseInt(document.getElementById("t8").value);
  var result = input1 - input2;
  document.getElementById('subtract-result').innerHTML = `<i style="color: blue">${result}</i>`;
}
<div>
  <h1>Addition</h1>
  <input type="text" id="t1" name="t1"> +
  <input type="text" id="t2" name="t2">
  <input type="button" id="add" value="=" onClick="add();">
  <span id="add-result"></span>
</div>
<div>
  <h1>Subtraction</h1>
  <input type="text" id="t7" name="t7"> -
  <input type="text" id="t8" name="t8">
  <input type="button" id="subtract" value="=" onClick="subtract();">
  <span id="subtract-result"></span>
</div>
<div>
  <h1>Multiplication</h1>
  <input type="text" id="t5" name="t5"> *
  <input type="text" id="t6" name="t6">
  <input type="button" id="multiply" value="=" onClick="multiply();">
  <span id="multiply-result"></span>
</div>
<div>
  <h1>Division</h1>
  <input type="text" id="t3" name="t3"> ÷
  <input type="text" id="t4" name="t4">
  <input type="button" id="divide" value="=" onClick="divide();">
  <span id="divide-result"></span> |
  <span id="divide-remainder"></span>
</div>

It's worth noting, with innerHTML there are security concerns as mentioned here:

...there are ways to execute JavaScript without using elements, so there is still a security risk whenever you use innerHTML to set strings over which you have no control. For example:

const name = "<img src='x' onerror='alert(1)'>";
el.innerHTML = name; // shows the alert

For that reason, it is recommended that you do not use innerHTML when inserting plain text; instead, use Node.textContent. This doesn't parse the passed content as HTML, but instead inserts it as raw text.


Here are some other methods used to manipulate the DOM:

See the full list here.

As you have discovered, document.write() is tricky because it has a tendency to overwrite the existing content when used. Let's see what the documentation has to say about it:

Note: Because document.write() writes to the document stream, calling document.write() on a closed (loaded) document automatically calls document.open(), which will clear the document.

Hmm, well what else can we do? Fortunately, it is possible to target specific parts of the page and replace content in those elements only. So, for example we could add <span> elements with ids like #add-result, #div-result etc to the page which will contain the results of their respective action. Then, instead of using document.write() to output the results, we can replace the content in those elements.

Let's add a <span> to contain our add result:

      Addition<p>
            <input type="text" id="t1" name="t1" /> +
            <input type="text" id="t2" name="t2" />
            <input type="button" id="add" value="=" onClick="add();" />
            <span id="add-result></span>
            </p>

(Note: remember to close your <input /> tags with a />!)

How do we target a specific element? With document.querySelector(). Docs

Then, we can easily change the content inside of that element by updating the element.textContent property, just like you would a variable:

    function add(){
        var input1 = parseInt(document.getElementById("t1").value);
        var input2 = parseInt(document.getElementById("t2").value);
        var result = input1+input2;
        
        // get the element with the #add-result ID
        var element = document.querySelector("#add-result");

        // update the content in that element
        element.textContent = result;
    }

Now when you add two numbers together and press =, the result will appear inside of the <span id="add-result"></span> element instead of overwriting the entire page. See if you can get it working for the other inputs as well. Remember you will need a unique id for each element you want to display a result in, and update the calculation functions accordingly!

Related