3

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>
1
  • use the function keyword else you will get error that myfunc() is not defined. Commented Aug 4, 2020 at 4:00

2 Answers 2

4

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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

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.