473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Increase displayed variable after clicking button

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
3 6868
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob | last post by:
I am displaying a form with a datagrid populated from a select of database records which now exceed 12,000 when I select them all. The form displays just fine with all 12,000+ entries but when I...
1
by: Bob | last post by:
I am displaying a form with a datagrid populated from a select of database records which now exceed 12,000 when I select them all. The form displays just fine with all 12,000+ entries but when I...
6
by: Osama Rajab | last post by:
Hi Guys, When I skim fast into my old code or others code, I forget what the type of a certain variable was or what its initialization value was. We use the Hungarian notation and it helps to...
1
by: Joseph Luner | last post by:
I am having problem postback, (code shown below) the variable "my_str" is lost after clicking the "submit" button. Isn't it suppose to display "changed" after posting back? Is there anyway I...
3
by: Helpseeker | last post by:
Hi all, I have written a small code in which i declare a static int variable and increment its value by one each time i click on a button. actually i use the int variable value in a particular URL...
8
by: Phoebe. | last post by:
Hi, Good Day! I need to create a variable that it's value can be carry across within a form. I've created some above the "web form designer generated code" but it seems not working. It...
7
by: MgGuigg | last post by:
Hello all, This is my first time posting a question to this forum, so here is hoping I am following protocol. I am scraping the rust off my old Basic programming skills, and have just recently...
11
by: eomer | last post by:
My site is very small, and plain - it will mostly be text (for now). I have used a good mix of css in the design. With usability in mind I would like to add some specific functionality (with the...
25
by: Jonno | last post by:
Hi I am having a problem with session vars being propagated between pages on this site: http://www.meettheancestors.com/sessiontest/index.php If you enter any user id and password and click Log...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.