1

I'm working on a javascript exercise to create a guessing game. I'm doing a bit of testing of the function before moving onto the next stage but this isn't working. When you click the button it's not giving the pop-up "Right Answer!". Any ideas?

<html>
<head>
<script>
var userguess = "5";
var answer = "5"; 

function Guess(){
if (userguess==answer){

alert("right answer!");
} else {

alert("wrong answer!");
} 
}
</script>
</head>
<body>
<form>
<input id="Guess" type="text" />
<button name="button" onClick="Guess()">Click me</button>
</form>
</body>
</html>
6
  • try onclick use lowercase C Commented Feb 6, 2015 at 9:04
  • @Matjaž, you don't have to guess just because the question is about a guessing game. Commented Feb 6, 2015 at 9:08
  • 3
    because when you are clicking on the button the form is submitted and the page reloads. Commented Feb 6, 2015 at 9:08
  • Ashad is right, use <button type="button"> to alleviate this (or use the side effect in Developer's answer). Commented Feb 6, 2015 at 9:09
  • I've changed it to the above and now getting Uncaught TypeError: object is not a function when debugging in Chrome. This is on this line: <button type="button" type="button" onClick="Guess()">Click me</button> Commented Feb 6, 2015 at 9:22

6 Answers 6

5

There are two problems:

  • The button action is "submit" change it with: type="button"

  • Your input has the id "Guess", this conflicts with the function name.

So when you change the type of the button and rename the function it will work.

Sign up to request clarification or add additional context in comments.

Comments

3

When you click on button that is inside form, it will submit form and page will reload. To fix that, use type="button". It will prevent button to act as submit button

<form>
    <input id="Guess" type="text" />
    <button name="button" type="button" onClick="Guess()">Click me</button>
</form>

2 Comments

I've altered the code to this but it doesn't seem to be giving me the pop-up still and when debugging in chrome i'm getting: Uncaught TypeError: object is not a function. This is on this line: <button type="button" type="button" onClick="Guess()">Click me</button>
This answer is correct but solves only one of the two problems this code has. See my answer.
1

It's your form it's trying to submit the value:

http://jsfiddle.net/70ag6ang/

<head>
   <script>
     var userguess = "5";
     var answer = "5"; 

     function Guess(event){

     if (userguess==answer){

          alert("right answer!");
      } else {

     alert("wrong answer!");
    } 
}
</script>
</head>
<body>

<input id="Guess" type="text" />
<button name="button" onclick="Guess()">Click me</button>

</body>

Comments

0

It's because the button triggers the form to be submitted.

Also, you could use addEventListener method:

<html>
  <head>
    <script>
    var userguess = "5";
    var answer = "5"; 

    document.addEventListener('DOMContentLoaded', function() {
      document.querySelector('.my-button').addEventListener('click', function(event) {
        event.preventDefault();
        if (userguess==answer){
          alert("right answer!");
        } else {
          alert("wrong answer!");
        } 
      });
    }, false);

    </script>
  </head>
<body>
    <form>
      <input id="Guess" type="text" />
      <button name="button" class="my-button">Click me</button>
    </form>
  </body>
</html>

The first addEventListener listens when all the DOM element were added. Alternatively, you can put your <script> tag after <body> element.

The second one listens to click event on your element that you found using document.querySelector. Note, that I've added a class my-button to the element.

Comments

0

the problem is the Function name conflicts with the input id

Try following code

Html

<body>
<form>
<input id="Guess" type="text" />
<button name="button" type="button" onClick="guess()">Click me</button>
</form>
</body>

Javascript

var userguess = "5";
var answer = "5"; 

function guess(){
if (userguess==answer){

alert("right answer!");
} else {

alert("wrong answer!");
} 
}

You can check Here

http://jsfiddle.net/L1x184nk/

4 Comments

Doesn't work when onclick event handler changed from guess to Guess!
Yes, because it conflicts with the input id. See my answer.
@Rakesh_Kumar yes Johni is correct , If you want to use Guess juest change id of text field, anyway normally javascript functions are starting with small letters
You could also use some kind of prefix for html elements. For example: txtGuess, btnSend etc. Yes it's stolen from Visual Basic ;-)
-2

    var userguess = "5";
    var answer = "5";
    function GuessFn() {

      if (userguess == answer) {

        alert("right answer!");
      } else {

        alert("wrong answer!");
      }
    }
    <form>
      <input id="Guess" type="text"/>
      <button name="button" onclick="GuessFn()">Click me</button>
    </form>

UPDATE: After reading through comments in OP thread I noticed his Element ID is "Guess" as well as the function name is "Guess" so this could be the reason behind "object is not a function" error. I also removed my ambiguous statement as in discussion below in comments to my answer.

2 Comments

he says his function is not getting fired, not if condition inside it
Okay, I probably made an ambiguous statement for which I got the negatives. What I meant to say is === compares values but also the type of variable. == compares by converting to common type info here. But anyways you're right OP is asking his function is not getting fired onclick.

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.