1

Just trying to run a simple function when the button "calculate" is pressed. Function won't run at all when inputs are in a form, ultimately I want to be able to modify the other inputs when the calculate button is pressed. Any help at all please!

function calculate() {
  alert("called");
}
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<form name="form">
  Price:
  <input name="priceCAD" value="0">
  <br>
  <br>Markup:
  <input name="percentage" value="0">
  <br>
  <br>Fiat:
  <input name="fiat" value="0">
  <br>
  <br>BTC:
  <input name="btc" value="0" maxlength="11">
  <br>
  <br>

  <input type="button" onClick="calculate()" name="calculate" value="Caculate">
  <input type="button" name="clear" value="Clear" onClick="form.fiat.value=0, form.btc.value=0, form.markup.value=0">
</form>

2 Answers 2

1

Button HTML just need it:

<input type="button" onClick="calculate()" name="calculate" value="Caculate" >

And don't forget insert jquery lib in your <head>.

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

6 Comments

I left document.calculate by accident in there. Where do I insert jquery lib? In all the examples i've been going off of online I have not seen that once.
Between <html> and <body>. Like this: <html lang="en-US"> <head> <script src="ajax.googleapis.com/ajax/libs/jquery/1.11.3/…> </head> <body>
Made the changes and still nothing. If I get rid of the form tags it calls on the function no problem. However with the 2 form tags in it doesn't get to the function.
Look like have conflict at function javascript name and input name.
Yes that was the issue thanks! Can't have a function name and element name the same.
|
0

function calculate() {
  alert("called");
}

document.getElementsByName("calculate")[0].addEventListener("click", calculate);
<form name="form">
  Price:
  <input name="priceCAD" value="0">
  <br>
  <br>Markup:
  <input name="percentage" value="0">
  <br>
  <br>Fiat:
  <input name="fiat" value="0">
  <br>
  <br>BTC:
  <input name="btc" value="0" maxlength="11">
  <br>
  <br>

  <input type="button" name="calculate" value="Caculate">
  <input type="button" name="clear" value="Clear" onClick="form.fiat.value=0, form.btc.value=0, form.markup.value=0">
</form>

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.