• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Simple Javascript Problem


Active Member
I'm just learning javascript and getting my head around it but there's a simple problem I've encountered.

Using document.write within a function that doesn't specify where to write it seems to fail.

I'm starting at the basics and I'm trying to have get the input from an input box and simply write it, after the submit button has been clicked.

The code I've got at the moment (which doesn't work):

<script type="text/javascript">
function print_number()
var num = document.getElementById('input-number').value;

Put your high number in here: <input type="text" id="input-number" />
<input type="button" value="submit" onclick="print_number()" />


I think the problem is that it doesn't know where to write the new variable?

Worth noting it works as an alert, but that's not quite what I'm after.


Junior Member
Hi Fred

document.write runs as the page loads, so you can't use that to change values on pages that have already loaded.

However you could use the onClick method to change an existing text value in a <div id="x">Value</div>.

You should find this info in a basic AJAX tutorial...


Active Member
Hey Allen.

Totally forgot about this thread, I really should stop doing that.

Thankfully the problem is sorted, using getElementById and .innerHTML worked a treat.

The code above was working towards a Prime Number Calculator, which you can see here, albeit in the Beta stage.