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?
-
1You may want to look into one of the various JavaScript template engines if you need that feature a lot.Pointy– Pointy2013年01月01日 17:30:45 +00:00Commented Jan 1, 2013 at 17:30
-
$('#textArea').html('<p>'+texts[textNumber]+'<p>');A. Wolff– A. Wolff2013年01月01日 17:36:14 +00:00Commented Jan 1, 2013 at 17:36
1 Answer 1
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
Gabriel Gartz
2,87024 silver badges24 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Gabriel Gartz
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
Explore related questions
See similar questions with these tags.
default