...

View Full Version : how to show a number greater than earlier?



moinism
07-19-2011, 03:11 PM
Hi Everyone.!
I have to create a quite simple javascript but being a newbie I'm having trouble in it.
It's a javascript about showing a value greater than the earlier for a variable.
Let me show you.


var count=2o
var countNow=count+1
What should I use here to show 21


Please help me.

alykins
07-19-2011, 03:14 PM
alert("countNow");

moinism
07-19-2011, 03:18 PM
alert("countNow");
Hey thanks for quick reply but I'm willing to show countNow in document.write . how should i do that?

alykins
07-19-2011, 03:21 PM
check out response number 2 in this (http://www.codingforums.com/showthread.php?t=232260) post where he has document.write highlighted

moinism
07-19-2011, 03:33 PM
Sorry..!
Still can't understand.
Is there any other way to do this?

rnd me
07-19-2011, 05:56 PM
Hey thanks for quick reply but I'm willing to show countNow in document.write . how should i do that?

huh?


document.write(countNow);

Philip M
07-21-2011, 07:57 AM
document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

Use DOM methods for display:-


<span id = "num">20</span><br><br>
<input type = "button" value = "Add One" onclick="addOne()">

<script type = "text/javascript">
var countNow = 20
function addOne() {
countNow = countNow+1;
document.getElementById("num").innerHTML = countNow;
}
</script>


"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

jmlwebdesign
07-21-2011, 10:23 AM
document.write(countNow);

Krupski
07-22-2011, 01:04 AM
document.write(countNow);

document.write is baddddd.........

In reference to the OP's code...


var count=2o
var countNow=count+1
What should I use here to show 21

This could be done:

<!-- results will be displayed here -->
<p id="one"></p>
<p id="two"></p>
<p id="three"></p>

<script type="text/javascript">
var one = document.getElementById('one');
var two = document.getElementById('two');
var three = document.getElementById('three');
var count = 20;
one.innerHTML = 'The initial value of count is: ' + count;
var countNow = count + 1;
two.innerHTML = 'The value of count is now: ' + count;
three.innerHTML = 'The value of countNow is: ' + countNow;
</script>


Kinda clumsy, but it clearly shows what is going on as well as showing the proper way to display things (as opposed to "document.write").



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum