Skip to content Skip to sidebar Skip to footer

Javascript Function Not Running?

Okay, so me and my friend are rather new to programming, we have some experience, but not too much, and I'm sure we're just making a stupid mistake here. We are attempting to make

Solution 1:

The main problem is that your global var userChoice has the same name as the function's argument. The function getUserChoice over writes it's parameters value with the values set in the conditionals. Then it does nothing with it; once you leave the function's scope, it's lost.

If you want the function to operate on the global variable, don't give the function a parameter with the same name.

The second problem is that you assign the global varible userChoice the return value of your function, and since it doesn't return anything, it actually over writes the value with undefined. So even if you did the above, it still wouldn't work.

Fourth document.userInput isn't defined. You need to query for it, e.g. document.getElementById('userInput').

All in all, a better solution would be to do this:

<html><head><script>var userChoice;

            var setUserChoice = function(event) {
                event.preventDefault();
                var choices = event.target.userChoice;

                for (var i =0; i < choices.length; i++) {
                    if (choices[i].checked) {
                        userChoice = choices[i].value;
                    }
                }

                event.target.choice.value = userChoice;
            }

            window.onload = function() {
                var form = document.getElementById('userInput');
                form.addEventListener('submit', setUserChoice, false);
            };
        </script></head><body><formid="userInput"><inputtype="radio"name="userChoice"id="userChoice_rock"value="rock">Rock</input></br><inputtype="radio"name="userChoice"id="userChoice_paper"value="paper">Paper</input></br><inputtype="radio"name="userChoice"id="userChoice_scissors"value="scissors">Scissors</input></br><inputtype="radio"name="userChoice"id="userChoice_lizard"value="lizard">Lizard</input></br><inputtype="radio"name="userChoice"id="userChoice_spock"value="spock">Spock</input></br><inputtype="submit"value="Enter" /></br><outputname="choice"></output></form></body></html>

Solution 2:

After that you are calling the getUserChoice method ant the beginning even after the user make his real choice you have to attach this function to the clic event something like this:

var submit_button = document.getElementById('mybutton');
submit_button.addEventListener('click', function() {
    getUserChoice();
}, false);

Solution 3:

ok, 2 things, first i needed to put (), without my firefox dont call the function

<formname="userInput"action="javascript:getUserChoice()">

second, you need to change:

if (document.userInput.getElementById('userChoice_rock').checked)

to

if (document.getElementById('userChoice_rock').checked)

since userInput is not a member of document

if you would now put

alert(userChoice);

at the end of the function, you will get the userChoice on clicking the submit button

Post a Comment for "Javascript Function Not Running?"