JavaScript will not output a value to a function

Hi, I am very new to Javascript. In the code below, when I test, I do not see any output. Can anyone tell me what is wrong?

  

    <script>
        function myFunction()
        {
            var x="";
            var score=document.myscore.score.value;

            if (score>30)
            {
                x="Expert";
            }
            else
            }
                x="Novice";
            }
            document.write(x);
        }
    </script>
</head>
<body>
    <form name="myscore">
        Score: <id="score"> <input type="number" name= "score">
        <input type="submit" onClick="myFunction()" value="submit">

    </form>


</body>

+4
source share
5 answers
<script>
function myFunction()
{
var x="";
var        score=document.myscore.score.value;

if (score>30)
        {
            x="Expert";
        }
        else
        {
            x="Novice";
        }
        document.write(x);
    }
</script>
</head>
<body>
<form name="myscore">
Score: <id="score"> <input type="number" name= "score">
<input type="submit" onClick="myFunction()" value="submit">

</form>


</body>

That should work! The problem was orientation (after else.

+1
source

Few things:

  • <id="score"> <input type="number" name= "score">probably not what you mean. Maybe you mean <input type="number" name="score" id="score">.
  • document.write- this is no-no, please never use it. It allows you to modify the document "here and now", that is, at boot time. For example, use document.body.innerHTML += x.
  • var score = document.getElementByID('score').value
  • Bracket after the sentence else
+1
source

:

  <script>
    function myFunction()
    {
        var x;
        var score=document.getElementById("in_score").value;

        if(score>30)
          x="Expert";
        else
          x="Novice";
        document.getElementById("score").value = x;
    }
  </script>
  <body>
    <form name="myscore">
     Score: <input id="score" type="number" name= "score">
     <input type="submit" id="in_score" onClick="myFunction()" value="submit">

    </form>


   </body>
   </html>

, ​​ else. , / , getElementById ( ) .value, / .

0
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>enter code here
 <script>
        function myFunction()
        {
            var x="";
            var score=document.myscore.score.value;

            if (score>30)
            {
                x="Expert";
            }
            else
             {
                x="Novice";
            }
            document.write(x);
        }
    </script>
</head>
<body>
    <form name="myscore">
        Score: <id="score"> <input type="number" name= "score">
        <input type="submit" onClick="myFunction()" value="submit">

    </form>


</body>
</html>
0
  • submit button , .
  • <p > < ID = "" > p > } { else
  • document.write alert console.log
0

Source: https://habr.com/ru/post/1689519/


All Articles