1
var userChoice;
$(document).ready(function(){
$("#rock").click(function(){
  userChoice = "rock";
});
$("#paper").click(function(){
    userChoice = "paper";
});
$("#scissors").click(function(){
    userChoice = "scissors";
});
});
document.write(userChoice);

I am trying to get the user output after the click event. But when I run this code I get the output as "undefined". I am very new to programming.

3
  • 5
    It's 2017 - document.write belongs to a previous millennium, let alone century. Commented Feb 10, 2017 at 10:48
  • you're right @Jamiec, but we are talking with a really big newbie! Commented Feb 10, 2017 at 11:22
  • @misterwolf even more reason to kill bad habits early. Commented Feb 10, 2017 at 12:00

7 Answers 7

3

Firstly, don't use document.write. It's considered very bad practice. Instead amend the text of a DOM element to show the output.

Secondly, the problem is because you only check the value of userChoice on load of the page. You need to instead check it under each of the click events, like this:

$(document).ready(function() {
  $("#rock").click(function() {
    var userChoice = "rock";
    $('#output').text(userChoice);
  });
  
  $("#paper").click(function() {
    var userChoice = "paper";
    $('#output').text(userChoice);
  });
  
  $("#scissors").click(function() {
    var userChoice = "scissors";
    $('#output').text(userChoice);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="rock">Rock</button>
<button id="paper">Paper</button>
<button id="scissors">Scissors</button>
<div id="output"></div>

Note however that you can improve this code further by using the DRY (Don't Repeat Yourself) principle. To do that apply a class to all the elements and use a single event handler:

$(document).ready(function() {
  $(".choice").click(function() {
    userChoice = this.id;
    $('#output').text(userChoice);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="rock" class="choice">Rock</button>
<button id="paper" class="choice">Paper</button>
<button id="scissors" class="choice">Scissors</button>
<div id="output"></div>

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

3 Comments

Ah... Beat me to it
This is what I was looking for. Thank you so much!
Btw! I am using font awesome icons for the rock, paper, and scissors. And they already have a class. How do I use another class with it?
1

You have to put printing inside click event. When page loads, document.write fires immediately and its null at that time.

var userChoice;
$(document).ready(function(){
$("#rock").click(function(){
  userChoice = "rock";
  console.log(userChoice);//THIS WAY
});
$("#paper").click(function(){
    userChoice = "paper";
});
$("#scissors").click(function(){
    userChoice = "scissors";
});
});

Comments

1

This is because,

document.write(userChoice);

executes on page load and userChoice gets the new value when some click action performs.

So try this:

$("#scissors").click(function(){
    userChoice = "scissors";
    console.log(userChoice);
});

Note: Do not use document.write() as this will make your entire page blank and show only text in userChoice

1 Comment

Oh yes! But how do I print the new value outside the function. Can you help me?
0

Put document.write() inside the click callback functions:

var userChoice;
$(document).ready(function(){
$("#rock").click(function(){
  userChoice = "rock";
  document.write(userChoice);
});
$("#paper").click(function(){
    userChoice = "paper";
    document.write(userChoice);
});
$("#scissors").click(function(){
    userChoice = "scissors";
    document.write(userChoice);
});
});

document.write() gets called once the page has loaded and only this time. But as you didn't give userChoice a value on initialization, it's value is undefined. When you click on one of the buttons, the value of userChoice gets changed, but document.write() gets not called again.

Comments

0

Replace your functions by only one and get the choice by using innerHTML or change it where the value is.

Your document.write(userChoice); will be executed before the click events and will not be updated on click

var userChoice;
$(document).ready(function() {
  $(".choice").click(function(){
    userChoice = this.innerHTML;
    console.log(userChoice);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="choice">rock</p>
<p class="choice">paper</p>
<p class="choice">scissors</p>

Comments

0
  1. never execute document.write after page load. Instead update some tag's .html()
  2. you execute your write before you click anything.

You likely mean

var userChoice = "";
$(document).ready(function() {
  $(".hitme").click(function() {
    userChoice = this.id;
    $("#choice").html(userChoice);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="hitme" type="button" id="rock">Rock</button>
<button class="hitme" type="button" id="paper">Paper</button>
<button class="hitme" type="button" id="scissors">Scissors</button>
<span id="choice"></span>

Comments

0

I suppose, better do not give you the final code but more explanation about code execution:

Do not use

document.write

it's dangerous, it can rewrite whole html page (when the page is already loaded).

The output is set on "undefined" because the variable

userChoice

is not still initialized, it will be initialized once user triggers an event: in your case a click on the divs. So just move the "userChoice" print/elaboration

 (eg: $('.some_div').innerHTML = userChoice) 

into an eventHandler, like one of yours:

$("#scissors").click(function(){ .... })

Read this guide for better info about javascript execution/running time: http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/. If you will like it and coding in general maybe in future you will post other interesting questions about Javascript!

Comments

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.