Using <form> and <input> elements as an input for JavaScript code. Is this the best way to do this?

后端 未结 2 491
悲哀的现实
悲哀的现实 2021-01-28 18:26

Folks, I am newbie to coding, obviously, so having completed few Lynda courses recently on HTML and Javascript, I have hit a wall with my simple HTML page. Basically, what I wan

相关标签:
2条回答
  • 2021-01-28 18:47

    Use solution from singe31, but to prevent reloading of the page after click change type of your button from "submit" to "button" or place "return false;" as the last line of calculateError() function. Otherwise you won't see any change on the page.

    0 讨论(0)
  • 2021-01-28 19:02

    As Satpal said in the comment you should indeed cast your text to an int with :

    var exactMass = parseInt(document.getElementById("exactMass").value);
    var accurateMass = parseInt(document.getElementById("accurateMass").value);
    var ppmError=(accurateMass-exactMass)/exactMass*1000000;
    

    For your other question, in your html add an empty paragraph where you want to display your result

    <p id="result"></p>

    And instead of calling console.log you can do :

    document.getElementById('result').innerHTML = ppmError;
    
    0 讨论(0)
提交回复
热议问题