Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  47] [ 4]  / answers: 1 / hits: 23036  / 11 Years ago, wed, october 16, 2013, 12:00:00

I'd like to show a random numbers within my list. Im grabbing the list from the backend, and im just using these numbers as examples. This is my code, I'm not quite sure why its not filling in the text.



setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6);
$('.num-gen').text(number);
},


Made a fiddle here to show you what I'm working with. Thanks in advance.


More From » jquery

 Answers
28

You had an error in your code, you were missing the finishing , 1000/*time in ms*/);!



Also, jQuery is not required here, you may do this in Javascript like this.



setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6);
document.getElementsByClass('num-gen')[0].innerHtml = number;
}, 1000);


EDIT:



setInterval(function() {
jQuery.each(jQuery('.num-gen'),function(){
var number = 1 + Math.floor(Math.random() * 6);
jQuery(this).text(number);
});
}, 1000);

[#74938] Tuesday, October 15, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tristab

Total Points: 735
Total Questions: 106
Total Answers: 96

Location: Grenada
Member since Sun, Dec 20, 2020
3 Years ago
;