Increase displayed variable after clicking button

Z

Zeljko

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
 
V

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

Daniel Kirsch

Zeljko said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top