Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a question and I will be very happy if you can help me! My question is related to html and javascript. I made something like a wheel of fortune, but after pressing the "spin" button I want a window to appear in which to enter information and according to this information I want a specific value to fall. I used an IF statement and a switch statement, but nothing came of it. Can you help me solve my problem please? I will be glad if you can!

What I have tried:

This is the JavaScript code I use:

function myfunction(){
var x = 1024; //min value
var y = 9999; //max value

var deg = Math.floor(Math.random() * (x - y)) + y;
document.getElementById('box').style.transform = "rotate(" + deg + "deg)";

var element = document.getElementById('mainbox');
element.classList.remove('animate');
setTimeout(function(){
element.classList.add('animate');

},5000); //5000 = 5s from css mainbox


var span1 = document.getElementById('s1');
var span2 = document.getElementById('s2');
var span3 = document.getElementById('s3');
var text;
var number = prompt("Plese enter your number:").value;

switch(number){
case 1:
text = span3;
break;
case 2:
text = span3;
break;
case 3:
text = span1;
break;
}
}

But it doesn't recognize the id path I give it when I use a switch statement. So can you help me to solve my problem, please?
Posted
Updated 19-Feb-21 4:28am
v2

EDIT noticed this issue after I posted other
JavaScript
var number = prompt("Plese enter your number:").value;

The .value thing is wrong.

You need to change it to :
JavaScript
var number = prompt("Plese enter your number:");


########################################

Where did you define the JS?

Does JS only run after onload() of page?

If it runs before the page is completely done loading then it will not find the elements (marked by your ids).

Also, if you are popping up a window and then running the code, the new window may not have the element ids it's looking for.

Most likely you are running hte code before onload. THis is a common mistake.

We'd have to see the main HTML page to know more about the problem.
 
Share this answer
 
v2
This is the body of the Html code:
"
HTML
<body>
    <div id="mainbox" class="mainbox">
        <div id="box" class="box">
            <div class="box1">
                <span id="s1" class="span1">6</span>
                <span id="s2" class="span2">5</span>
                <span id="s3" class="span3">4</span>
                <span id="s4" class="span4">3</span>
            </div>
            <div class="box2">
                6
                5
                4
                2
            </div>
        </div>
        <button class="spin" onclick="myfunction()">SPIN</button>
    </div>
    <script src="./js/function.js"></script>
</body>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900