Click here to Skip to main content
15,910,083 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi all,
I want to write count of how many times i clicked the button but nothing is shown on textbox.
Can anyone tell me how to show the count in textbox?
Thanks in advance.
<html>
<head>
<script type="text/javascript">
function init()
{ct=0;
function count()
{ct++
document.forms["f1"].elements["count"].Value=ct;}
}
</script>
</head>
<body>
<form name="f1">
<input type = "button" name="button2"  value="Check Count" = "count()">
   <input type = "text" name="count" size="5">
</form>
</body>
</html>
Posted
Updated 5-Apr-11 21:13pm
v2
Comments
Prerak Patel 6-Apr-11 3:13am    
Use pre tags for code blocks.

1 solution

XML
<html>
<head>
<script type="text/javascript">
function init()
{ct=0;
function count()
{ct++
document.forms["f1"].elements["count"].Value=ct;}
}
</script>
</head>
<body>
<form name="f1">
<input type = "button" name="button2"  value="Check Count" = "count()">
   <input type = "text" name="count" size="5">
</form>
</body>
</html>


You really expect this to work? with
missing } in init
missing ; for ct++
and something unknown like "Check Count" = "count()"

Try this

XML
<html>
<head>
<script language="JavaScript">
var count=0;
function myFun(f) {
count += 1;
f.myText.value = count;
}
</script>
</head>
<body>
<form>
<input type="text" name="myText" value=""><br>
<input type="button" name="myBut" value="Click" onClick="myFun(form)">
</form>
</body>
</html>
 
Share this answer
 
v2
Comments
Farah Siraj 6-Apr-11 3:26am    
@ missing } in init
there is complete closing of "}" for both functions.
it doesn't make difference either to write ";" or not because i wrote ct++ in separate line.
yes there was i mistake in "Check Count" = "count()" but i have corrected it as
<pre><input type = "button" name="button2" value="Check Count" önClick= "count()">
</pre>
but still it is not working.
Any solution ???
Prerak Patel 6-Apr-11 3:29am    
I've provided solution at the end. Check that.
Prerak Patel 6-Apr-11 3:28am    
I've provided solution at the end. Check that.
Farah Siraj 6-Apr-11 3:34am    
I have check your code but it is also not working.
Have you checked it?
Prerak Patel 6-Apr-11 4:26am    
definitely I have checked it, and works perfectly fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900