Simple Javascript Problem

Xenonsoft

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):

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

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

</form>
</body>

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.
 
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...
 
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.

Cheers.
 
Back
Top