Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call my Java Script function correctly?

Tags:

javascript

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>
like image 718
Sina Keshmiri Avatar asked May 03 '26 17:05

Sina Keshmiri


2 Answers

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>
like image 162
Gustavo Farias Avatar answered May 05 '26 06:05

Gustavo Farias


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

like image 23
claricetorres Avatar answered May 05 '26 08:05

claricetorres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!