0

Im pretty new to JS and HTML (started ~20 hours ago) and already have a problem: below you can see my code. As one tutorial said, clicking on button will change the statusLine text. But something went wrong and i cant figure it out.

    <!DOCTYPE html>
<html>
<head>
<title>Некое подземелье</title>
</head>
<body>
<p id="statusLine">Вы попали в подземелье.</p>

<button type="button" onclick="goDeeper()">Идти глубже в подземелье</button>

<script>
    function goDeeper()
     {
       var nextEvent=(Math.floor(Math.random()*10+1));
       switch(nextEvent){
        case'1':
            document.getElementById("statusLine").innerHTML="Вам на пути попался гоблин!";
            break;
                }
     }
</script>
</body>
</html>

So, something is wrong. What should i do in order to fix this?

1
  • What does the javascript console say? And what should happen if nextEvent is not '1' Commented Nov 3, 2013 at 20:24

2 Answers 2

3

Try making the case statement match the number 1 rather than the string '1':

function goDeeper()
{
    var nextEvent = Math.floor(Math.random()*10+1);
    switch(nextEvent) {
        case 1:
            document.getElementById("statusLine").innerHTML="Вам на пути попался гоблин!";
            break;
    }
}

Or for that matter, if there is only one condition you need to match, just get rid of the switch and use a simple if block:

function goDeeper()
{
    var nextEvent = Math.floor(Math.random()*10+1);
    if (nextEvent == 1) {
        document.getElementById("statusLine").innerHTML="Вам на пути попался гоблин!";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
What I understand you want to change the text on click button so Try this 

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="Hello World";
}
</script>
</head>
<body>

<p>Click the button to trigger a function.</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>`enter code here`

</body>
</html>

Refrence Link : http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick

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.