Sina Keshmiri
Sina Keshmiri

Reputation: 39

how to call my Java Script function correctly?

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>

Upvotes: 3

Views: 60

Answers (2)

claricetorres
claricetorres

Reputation: 128

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

Upvotes: 2

Gustavo Farias
Gustavo Farias

Reputation: 423

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>

Upvotes: 4

Related Questions