Reputation: 3
Using javascript, I defined a variable as one of three words
JavaScript:
$(document).ready(function () {
var things = ['Rock', 'Paper', 'Scissor'];
var thing = things[Math.floor(Math.random()*things.length)];
});
I want the variable "thing" to display within a sentence on my screen using the following in the HTML portion:
the computer chose <span id="thing></span>
If someone could help me figure out what's going wrong, I would appreciate it
Upvotes: 0
Views: 278
Reputation: 852
Other than the already suggested
$('#thing').html(thing);
You can also use
$('#thing').append(thing);
or even
$('#thing').text(thing);
I guess it all depends on the bigger picture of your program. I am currently learning jQuery myself. It's really something.
Upvotes: 0
Reputation: 24901
You need to set the HTML of the span. In your code you only created a variable, but you also need to use it somewhere in HTML. Since you are using jQuery you can use ID selector and html()
method to set the value:
$(document).ready(function () {
var things = ['Rock', 'Paper', 'Scissor'];
var thing = things[Math.floor(Math.random()*things.length)];
$('#thing').html(thing);
});
Upvotes: 2