i just started learning JS i cant find what is wrong with my code :
<!DOCTYPE html>
<html>
<body>
<script>
myfunc(){
var no1=parseInt(document.getElementById("n1").value);
var no2=parseInt(document.getElementById("n2").value);
document.getElementById("f").innerHTML=no1+no2;
}
</script>
num1: <input type="text" id="n1"/>+num2: <input type="text" id="n2"/>
<button onclick="myfunc()">=</button>
<p id="f"></p>
</body>
</html>
Just declare your function using the function reserved word
<!DOCTYPE html>
<html>
<body>
<script>
function myfunc(){
var no1=parseInt(document.getElementById("n1").value);
var no2=parseInt(document.getElementById("n2").value);
document.getElementById("f").innerHTML=no1+no2;
}
</script>
num1: <input type="text" id="n1"/>+num2: <input type="text" id="n2"/>
<button onclick="myfunc()">=</button>
<p id="f"></p>
</body>
</html>
<html>
<body>
<script>
function myfunc(){
var no1=parseInt(document.getElementById("n1").value);
var no2=parseInt(document.getElementById("n2").value);
document.getElementById("f").innerHTML=no1+no2;
}
</script>
num1: <input type="text" id="n1"/>+num2: <input type="text" id="n2"/>
<button onclick="myfunc()">=</button>
<p id="f"></p>
</body>
</html>
Hey there! You need to use the word function before myfunc() to define this function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With