<!DOCTYPE html >
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>AJAX with PHP: Quickstart</title>
    <script type="text/javascript" >

    var n = 1;
    function testNextPrime() {
        var isPrime = true;
        n = n + 1;
       // console.log('testing', n);
        for (var i = 2; i <= Math.sqrt(n); i += 1) {
            if (n % i == 0) isPrime = false;
        }

        if (isPrime) {
           document.getElementById("result").innerHTML='n';
          // document.getElementById("myDiv").innerHTML
        }

        // Schedule the next test (this gives browser a chance to update display and process any other events)
        setTimeout(testNextPrime, 200);
    }

    // Start testing for primes
    testNextPrime();

    </script>
    </head>
    <body>
        <p> I want to update prime number here</p>

    <div id="result" >

    </div>   
    </body>
    </html>



Hi I tried to update the div tag using above code. if I use document.write(n) instead of document.getElementById("result").innerHTML='n'; I can print the values. What is the wrong with above code?

the div is not rendered on the page yet.

move line 26 to in between line 36 and 35

...
</div>
<script>
    testNextPrime();
</script>
</body>
</html>

In addition to stbuchok's suggestion, on line 17, remove the single quotes around the n. n is a variable. The single quotes will treat it as a string.

Should be..

document.getElementById("result").innerHTML=n;