0

I wrote the code provided. But on clicking the button, nothing appears.

function calculate() {
  var colony = document.getElementById("co").value;
  var dilution = document.getElementById("dil").value;
  var inoculum = document.getElementById("in").value;
  var b = parseFloat(dilution) * parseFloat(inoculum);
  var c = parseFloat(colony) / b;
  if (!isNaN(c)) {
    document.getElementById("multiplication").innerHTML = "the conentration is " + c;
  }
}
<button type="button" onclick="calculate">Calculate</button>
<p id="multiplication"></p>

3

3 Answers 3

1
  1. You didn't call calculate() function. It should be onclick="calculate()" not onclick="calculate".
  2. There's no value property in an element. Use innerText property of an element.

function calculate() {
  var colony = document.getElementById("co").innerText;
  var dilution = document.getElementById("dil").innerText;
  var inoculum = document.getElementById("in").innerText;
  var b = parseFloat(dilution) * parseFloat(inoculum);
  var c = parseFloat(colony) / b;
  if (!isNaN(c)) {
    document.getElementById("multiplication").innerHTML = "the conentration is " + c;
  }
}
<button type="button" onclick="calculate()">Calculate</button>
<p id="multiplication"></p>
<div id="co">1</div>
<div id="dil">2</div>
<div id="in">4</div>

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

Comments

0

I have added the HTML, which wasn't provided in the shared code and it works. It is permissible for input fields to not have an ID attribute.

Client Side:

function calculate() {
    var colony = document.getElementById("co").value;
    var dilution = document.getElementById("dil").value;
    var inoculum = document.getElementById("in").value;
    var b = parseFloat(dilution) * parseFloat(inoculum);
    var c = parseFloat(colony) / b;
    if (!isNaN(c)) {
        document.getElementById("multiplication").innerHTML =
            "the conentration is " + c;
    }
}

HTML:

    co: <input type="text" id="co"><br>
    dil: <input type="text" id="dil"><br>
    in: <input type="text" id="in"><br>
    <button type="button" onclick="calculate()">Calculate</button>
    <div id="multiplication">fff</div>

Comments

0

In your inital code, your onclick="calculate" was missing () after calculate. I've added that as well as a script tag within your html with your JavaScript function without changing much of your original code's text.

Working Jsfiddle: https://jsfiddle.net/yuL58da1/

For presentation/educational purposes, I solved the problem using JQuery as well to show you what the solution would look like using JQuery.

Working JsFiddle: https://jsfiddle.net/4g9ayoqb/

Using the JQuery method, I've updated your html button to include a id attribute and removed onclick:

<button id="calculate" type="button">Calculate</button>

and added a JQuery click selector for that button's id:

$(document).on('click','#calculate',function()

2 Comments

The question contains no jQuery nor the jQuery tag
I've updated my answer showing both the Javascript way and the Jquery way to solve the problem. Just to show an example that JQuery could be used as well for educational purposes.

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.