bgmrk bgmrk - 4 months ago 8
Javascript Question

JavaScript help running a basic function on button click

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!

<HTML >
<html lang="en-US">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>



<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>

<script type="text/javascript">
function calculate (){
alert("called");
}
</script>


</body>
</HTML>

Answer

Button HTML just need it:

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

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