0
var texts = new Array("text1", "text2", "text2", "text3", "text4", "text5")
var textNumber = Math.floor(Math.random() * 10) + 1;
$('#textArea').html('<p>{texts[textNumber]}<p>');

I have this code, and i want this script to randomly choose an element of 'texts' array, and put it where the '#textArea' is, but interpolation doesn't seem to work, what is wrong here?

asked Jan 1, 2013 at 17:28
2
  • 1
    You may want to look into one of the various JavaScript template engines if you need that feature a lot. Commented Jan 1, 2013 at 17:30
  • $('#textArea').html('<p>'+texts[textNumber]+'<p>'); Commented Jan 1, 2013 at 17:36

1 Answer 1

3

Just a better code to do what you seen you are trying to do...

var texts = ["text1", "text2", "text2", "text3", "text4", "text5"]
var textNumber = Math.round(Math.random() * texts.length) ;
var text = texts[textNumber];
$('#textArea').html('<p>' + text + '<p>');
answered Jan 1, 2013 at 17:37
Sign up to request clarification or add additional context in comments.

1 Comment

I will understand that like a thanks for the help, hehe. But my friend, javascript is a lite bit different from some other languages that you have learned, it's very easy and powerfull, I can recommend a book called "Javascript the Good Parts - by Douglas Crockford", not everything he says ther is a absolut truth, but will sure help you understando how and why javascript works that way. Keep your researchs, I think you will enjoy a lot!shop.oreilly.com/product/9780596517748.do

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.