What's new

Javascript/HTML help

  • Thread starter michael1026
  • Start date
  • Views 505
michael1026

michael1026

Member
Forum Addict Mr. Nice Guy
Messages
3,720
Reaction score
436
Points
490
Sin$
7
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.
 
Xeren

Xeren

♦♦♦ God Complex ♦♦♦
Legendary Veteran Programmer Modder
Messages
5,668
Reaction score
2,107
Points
795
Sin$
0
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>
 
michael1026

michael1026

Member
Forum Addict Mr. Nice Guy
Messages
3,720
Reaction score
436
Points
490
Sin$
7
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.
 
Bar Starred

Bar Starred

Contributor
Grammar Nazi Grizzled Veteran
Messages
2,264
Reaction score
748
Points
270
Sin$
0
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.
 
michael1026

michael1026

Member
Forum Addict Mr. Nice Guy
Messages
3,720
Reaction score
436
Points
490
Sin$
7
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.
 
Bar Starred

Bar Starred

Contributor
Grammar Nazi Grizzled Veteran
Messages
2,264
Reaction score
748
Points
270
Sin$
0
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.
 
michael1026

michael1026

Member
Forum Addict Mr. Nice Guy
Messages
3,720
Reaction score
436
Points
490
Sin$
7
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.
 
michael1026

michael1026

Member
Forum Addict Mr. Nice Guy
Messages
3,720
Reaction score
436
Points
490
Sin$
7
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.
 
Bar Starred

Bar Starred

Contributor
Grammar Nazi Grizzled Veteran
Messages
2,264
Reaction score
748
Points
270
Sin$
0
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
Login
Register