What's new

Javascript/HTML help

  • Thread starter michael1026
  • Start date
  • Views 284
M

michael1026

Member
Messages
3,720
Reaction score
436
Code:
var whichFunction = function() {
    if((document.getElementById('five').checked) && (document.getElementById('tutorial').checked)) {
        both();
    } else if(document.getElementById('five').checked) {
        five();
    } else if(document.getElementById('tutorial').checked) {
        tutorial();
    } else {
        justHungry();
    }
};
 
var tutorial = function(randomNumber) {   
    var randomNumber = Math.floor(Math.random() * 8 + 1);
        switch (randomNumber) {
*Snip* Just cases
    }
};
 
 
 
var both = function() {   
        var randomNumber = Math.floor(Math.random() * 11 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
var five = function() {   
        var randomNumber = Math.floor(Math.random() * 10 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
 
var justHungry = function() {   
        var randomNumber = Math.floor(Math.random() * 6 + 1);
        switch (randomNumber) {
      // *snip* Just cases
    }
};
</script>
</head>
 
<body>
    <div class="body">
    <h1>Random Chooser</h1>
    <p>
    <p>
    <button onclick="whichFunction()" id="button">Choose For Me!!</button>
    <p><input type="checkbox" value="tutorial" name="tutorial" id="tutorial"></p>
    <p><input type="checkbox" name="five" value="five" id="five"></p>
    </p>
    </p>
    </div>
</body>
</html>
This used to work, then I started messing with the button, and I'm not sure what I did. I removed some text and CSS that wasn't important. But the problem is that the button won't run any functions. I even put a simple thing like Prompt(0); inside of the functions, but still won't do anything. I can post the page if needed. Thanks.
 
X

Xeren

♦♦♦ God Complex ♦♦♦
Messages
5,671
Reaction score
2,119
Code:
var whichFunction = function() {
    if((document.getElementById('five').checked) && (document.getElementById('tutorial').checked)) {
        both();
    } else if(document.getElementById('five').checked) {
        five();
    } else if(document.getElementById('tutorial').checked) {
        tutorial();
    } else {
        justHungry();
    }
};
 
var tutorial = function(randomNumber) {
    var randomNumber = Math.floor(Math.random() * 8 + 1);
        switch (randomNumber) {
*Snip* Just cases
    }
};
 
 
 
var both = function() {
        var randomNumber = Math.floor(Math.random() * 11 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
var five = function() {
        var randomNumber = Math.floor(Math.random() * 10 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
 
var justHungry = function() {
        var randomNumber = Math.floor(Math.random() * 6 + 1);
        switch (randomNumber) {
      // *snip* Just cases
    }
};
</script>
</head>
 
<body>
    <div class="body">
    <h1>Random Chooser</h1>
    <p>
    <p>
    <button id="button">Choose For Me!!</button>
    <p><input type="checkbox" value="tutorial" name="tutorial" id="tutorial"></p>
    <p><input type="checkbox" name="five" value="five" id="five"></p>
    </p>
    </p>
    </div>
</body>
</html>
This used to work, then I started messing with the button, and I'm not sure what I did. I removed some text and CSS that wasn't important. But the problem is that the button won't run any functions. I even put a simple thing like Prompt(0); inside of the functions, but still won't do anything. I can post the page if needed. Thanks.
Code:
document.getElementById('button').onclick = function() {
    if((document.getElementById('five').checked) && (document.getElementById('tutorial').checked)) {
        both();
    } else if(document.getElementById('five').checked) {
        five();
    } else if(document.getElementById('tutorial').checked) {
        tutorial();
    } else {
        justHungry();
    }
};
 
var tutorial = function(randomNumber) {
    var randomNumber = Math.floor(Math.random() * 8 + 1);
        switch (randomNumber) {
*Snip* Just cases
    }
};
 
 
 
var both = function() {
        var randomNumber = Math.floor(Math.random() * 11 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
var five = function() {
        var randomNumber = Math.floor(Math.random() * 10 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
 
var justHungry = function() {
        var randomNumber = Math.floor(Math.random() * 6 + 1);
        switch (randomNumber) {
      // *snip* Just cases
    }
};
</script>
</head>
 
<body>
    <div class="body">
    <h1>Random Chooser</h1>
    <p>
    <p>
    <button id="button">Choose For Me!!</button>
    <p><input type="checkbox" value="tutorial" name="tutorial" id="tutorial"></p>
    <p><input type="checkbox" name="five" value="five" id="five"></p>
    </p>
    </p>
    </div>
</body>
</html>
 
M

michael1026

Member
Messages
3,720
Reaction score
436
Code:
document.getElementById('button').onclick = function() {
    if((document.getElementById('five').checked) && (document.getElementById('tutorial').checked)) {
        both();
    } else if(document.getElementById('five').checked) {
        five();
    } else if(document.getElementById('tutorial').checked) {
        tutorial();
    } else {
        justHungry();
    }
};
 
var tutorial = function(randomNumber) {
    var randomNumber = Math.floor(Math.random() * 8 + 1);
        switch (randomNumber) {
*Snip* Just cases
    }
};
 
 
 
var both = function() {
        var randomNumber = Math.floor(Math.random() * 11 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
var five = function() {
        var randomNumber = Math.floor(Math.random() * 10 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
 
var justHungry = function() {
        var randomNumber = Math.floor(Math.random() * 6 + 1);
        switch (randomNumber) {
      // *snip* Just cases
    }
};
</script>
</head>
 
<body>
    <div class="body">
    <h1>Random Chooser</h1>
    <p>
    <p>
    <button id="button">Choose For Me!!</button>
    <p><input type="checkbox" value="tutorial" name="tutorial" id="tutorial"></p>
    <p><input type="checkbox" name="five" value="five" id="five"></p>
    </p>
    </p>
    </div>
</body>
</html>
Thank you, but it still doesn't work.
 
B

Bar Starred

Contributor
Messages
2,264
Reaction score
748
Code:
var whichFunction = function() {
    if((document.getElementById('five').checked) && (document.getElementById('tutorial').checked)) {
        both();
    } else if(document.getElementById('five').checked) {
        five();
    } else if(document.getElementById('tutorial').checked) {
        tutorial();
    } else {
        justHungry();
    }
};
 
var tutorial = function(randomNumber) {
    var randomNumber = Math.floor(Math.random() * 8 + 1);
        switch (randomNumber) {
*Snip* Just cases
    }
};
 
 
 
var both = function() {
        var randomNumber = Math.floor(Math.random() * 11 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
var five = function() {
        var randomNumber = Math.floor(Math.random() * 10 + 1);
        switch (randomNumber) {
//*Snip* Just cases
    }
};
 
 
 
var justHungry = function() {
        var randomNumber = Math.floor(Math.random() * 6 + 1);
        switch (randomNumber) {
      // *snip* Just cases
    }
};
</script>
</head>
 
<body>
    <div class="body">
    <h1>Random Chooser</h1>
    <p>
    <p>
    <button onclick="whichFunction()" id="button">Choose For Me!!</button>
    <p><input type="checkbox" value="tutorial" name="tutorial" id="tutorial"></p>
    <p><input type="checkbox" name="five" value="five" id="five"></p>
    </p>
    </p>
    </div>
</body>
</html>
This used to work, then I started messing with the button, and I'm not sure what I did. I removed some text and CSS that wasn't important. But the problem is that the button won't run any functions. I even put a simple thing like Prompt(0); inside of the functions, but still won't do anything. I can post the page if needed. Thanks.
You forgot to escape the comment in the tutorial function. You'd have found this issue in seconds, if you had looked at the JS console.
 
M

michael1026

Member
Messages
3,720
Reaction score
436
You forgot to escape the comment in the tutorial function. You'd have found this issue in seconds, if you had looked at the JS console.
It's not like that in the actual code. And in the JS console, it says whichFunction isn't defined. It looks like it is to me.
 
B

Bar Starred

Contributor
Messages
2,264
Reaction score
748
It's not like that in the actual code. And in the JS console, it says whichFunction isn't defined. It looks like it is to me.
When I copied your code, added '<head><script type="text/javascript">', and commented out the aforementioned issue. The script ran. It didn't do anything except fired off whichFunction, but it ran regardless. Funnily enough, it no longer told me that whichFunction was not defined. So, there are some things for you to look at.
 
M

michael1026

Member
Messages
3,720
Reaction score
436
When I copied your code, added '<head><script type="text/javascript">', and commented out the aforementioned issue. The script ran. It didn't do anything except fired off whichFunction, but it ran regardless. Funnily enough, it no longer told me that whichFunction was not defined. So, there are some things for you to look at.
http://michael1026.site90.com/LunchChooser.html Here is the website link.
 
M

michael1026

Member
Messages
3,720
Reaction score
436
Clicking "Choose for me!!", leads to 2 alerts, the first being '0', and the second suggests me a place to dine.
That's weird. It doesn't work for me or my friends.

Also, I put the 0 there to test it without another function.
 
B

Bar Starred

Contributor
Messages
2,264
Reaction score
748
That's weird. It doesn't work for me or my friends.

Also, I put the 0 there to test it without another function.
What browser are you using? What version of that browser? I'm assuming it's IE? I'm using the latest Chrome release, and it looks like it's working as expected.
 
Top Bottom