4

I am trying to make an array of strings and then pluck a random string and place it in a div with class "quote". Below is my current code.

$(document).ready(function() {

    var quotes = new Array("foo", "bar", "baz", "chuck");
    var randno = Math.floor ( Math.random() * quotes.length );
    $('.quote').add(quotes[randno]);

});

What am I doing incorrectly?

Thanks

3
  • 2
    What is populating the randno variable Commented Apr 1, 2012 at 0:31
  • Sorry, forgot to add that line. Just added it. Commented Apr 1, 2012 at 0:33
  • Its a JavaScript array, not a jQuery array. And it's better to use [...] instead of new Array(...) Commented Apr 1, 2012 at 8:01

1 Answer 1

11
$(document).ready(function() {
    var quotes = new Array("foo", "bar", "baz", "chuck"),
    randno = quotes[Math.floor( Math.random() * quotes.length )];
    $('.quote').text( randno );
});

try this

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

1 Comment

Haha literally RIGHT before I was gonna post. +1 for similar brainwaves jsfiddle.net/byu6V

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.