By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,068 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,068 IT Pros & Developers. It's quick & easy.

Increase displayed variable after clicking button

P: n/a
I am new to javascript (just started to learn it) and want to make a
simple thing.
I would like to make a page with only one button and one variable that
is displayed. What I want to do is that variable is increased every
time button is pressed (and, of course, that increased value is
displayed than at screen, so it could be increased again), something
like:

<html>
<body>
<script type="text/javascript">
function add(number)
{
return (number+1)
}
number=0
document.write('<input type="button" onclick="add('number')"
value="+">')
document.write(number)
</script>
</body>
</html>

but this is not working.

Zeljko

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
VK
> What I want to do is that variable is increased every
time button is pressed (and, of course, that increased
value is displayed than at screen, so it could be
increased again)


See <http://www.jibbering.com/faq/#FAQ4_15>

document.write() and document.writeln() if used after the page is
loaded *clear* the entire content of the page (including any script).
So the only time and place they don't do that is at the moment of the
arrival, so you can use them is the original page body, so your page
could self-adjust depending on the place of the arrival.

P.S. Rather difficult to express it clearly in human words, is it? From
my experience it usually means that there is something wrong with the
concept itself (?).

I would let people simply do:
someHTMLObject.write(hewHTMLContent);

What a hey difference anyway from:
someHTMLObject.innerHTML = hewHTMLContent;

At least the consistency would be kept.

Jul 23 '05 #2

P: n/a
Zeljko wrote:
I would like to make a page with only one button and one variable that
is displayed.


javascript:
var myNumber = 0; // better don't use number as your variable's name.
function add() {
myNumber++;
document.getElementById("variableVal").innerHTML = myNumber;
}
HTML:
<body>
<div id="variableVal">0</div>
<input type="button" onclick="add()" value="+">'
</body>

Daniel
Jul 23 '05 #3

P: n/a
Thanks (both of you)! It was just what I was looking for.

VK wrote:
What I want to do is that variable is increased every
time button is pressed (and, of course, that increased
value is displayed than at screen, so it could be
increased again)


See <http://www.jibbering.com/faq/#FAQ4_15>

document.write() and document.writeln() if used after the page is
loaded *clear* the entire content of the page (including any script).
So the only time and place they don't do that is at the moment of the
arrival, so you can use them is the original page body, so your page
could self-adjust depending on the place of the arrival.

P.S. Rather difficult to express it clearly in human words, is it? From
my experience it usually means that there is something wrong with the
concept itself (?).

I would let people simply do:
someHTMLObject.write(hewHTMLContent);

What a hey difference anyway from:
someHTMLObject.innerHTML = hewHTMLContent;

At least the consistency would be kept.


Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.