1

Im trying to auto calculate change using javascripts when i put some number in pay the change input will auto fill with change

<!DOCTYPE html>
<html>
<body>


subtotal: <input type="number" id="subtotal" value="20000">
pay : <input type="number" id="dibayar" onkeyup="kembalian()">

<p>change: <span id="demo"></span></p>

<script>
function kembalian() {
        var x = document.getElementById("subtotal").value;
        var y = document.getElementById("dibayar").value;
        var awe = x - y;
        console.log(awe);
document.getElementById("kembalian").innerHTML = awe;
        }

</script>

</body>
</html>



3 Answers 3

2

you need to insert it into demo id

function kembalian() {
  var x = document.getElementById("subtotal").value;
  var y = document.getElementById("dibayar").value;
  var awe = x - y;
  console.log(awe);
  document.getElementById("demo").innerHTML = awe;
}
subtotal: <input type="number" id="subtotal" value="20000"> pay : <input type="number" id="dibayar" onkeyup="kembalian()">

<p>change: <span id="demo"></span></p>

Sign up to request clarification or add additional context in comments.

Comments

0

first of, if you want to set your new value in "change" you should change the

document.getElementById("kembalian").innerHTML = awe;

to

document.getElementById("demo").innerHTML = awe;

You can also try changing onkeyup to onchange if that still not working.

Comments

0

Check the Id ! It should be : document.getElementById("demo").innerHTML = awe;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.