-2

I simply set up JSFiddle wrong, I'm new to Javascript and JSFiddle. Please forgive me.

When I open my app I want a random number appear. And I want a new random number to appear everytime I call the function. But so far I get the same number over and over again.

    function number(){
        var number= Math.floor(Math.random() * 230) + 90;
        document.getElementById("div").innerHTML = number;
    }

JSFiddle: http://jsfiddle.net/e6VFQ/3/

Carl
  • 1
  • 5
  • 2
    That code should work. Can you supply a fiddle that shows it not working? – Strikeskids Jul 12 '14 at 20:36
  • Sry typo, should be `.innerHTML` that's what I wrote in the actuall code. – Carl Jul 12 '14 at 20:41
  • You set up the jsfiddle wrong. Set the second dropdown on the left to "no wrap". http://jsfiddle.net/e6VFQ/4/ – JJJ Jul 12 '14 at 20:44
  • possible duplicate of [Simple example doesn't work on JSFiddle](http://stackoverflow.com/questions/5431351/simple-example-doesnt-work-on-jsfiddle) – JJJ Jul 12 '14 at 20:45
  • @Juhana You're right. I had no idea the problem was in JSFiddle. – Carl Jul 12 '14 at 20:50

2 Answers2

0

Here's the quick fiddle : http://jsfiddle.net/hepc4/

No major changes though. Changed the way to show number, just to prove that the random numbers are properly getting generated.

js

function number(){
        var number= Math.floor(Math.random() * 230) + 90;
        document.getElementById("no").value= number;
    }

html

<input type="text" id="no"/>
<input type="button" onclick="number()" value="Generate Rnd No"/>

Its working just fine.

Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43
0

Here's a working fiddle:

http://jsfiddle.net/KPSF3/1/

You don't get anything in your div because the "content" property applies only to :before and :after pseudo elements. You should use innerText:

document.getElementById("div").innerText = number;

Docs for content: https://developer.mozilla.org/en-US/docs/Web/CSS/content

Jon Snow
  • 3,682
  • 4
  • 30
  • 51