J N
J N

Reputation: 3

Trying to get javascript variable to display within a sentence in HT

Using javascript, I defined a variable as one of three words

http://jsfiddle.net/KDmwn/45/

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

Answers (2)

telex-wap
telex-wap

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.

html

append

text

Upvotes: 0

dotnetom
dotnetom

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

Related Questions