...

View Full Version : Js counter wht no work



yossidd
07-19-2012, 12:31 PM
THIS IS MY CODE



<html>
<head>
<script language="JavaScript">
var count=0;
function plus(f) {
count += 1;
f.myText.value = count;
}

function minus(f) {
if(f.myText.value>0)
{
count -= 1;
f.myText.value = count; }
}
</script>
</head>
<body>
<form>
<input type="text" name="myText" value=""><br>
<input type="button" name="myBut" value="+1" onClick="plus(form)">
<input type="button" name="myBut" value="-1" onClick="minus(form)">
</form>
</body>
</html>
input +2 buttons that make + and -
that work
what it want to do it 20 input like this and i dont want to copy the function
so... i tried to send the name of the text input in the function like this



<html>
<head>
<script language="JavaScript">
var count=0;
function plus(f,k) {
count += 1;
f.k.value = count;
}

function minus(f,k) {
if(f.k.value>0)
{
count -= 1;
f.k.value = count; }
}
</script>
</head>
<body>
<form>
<input type="text" name="myText" value=""><br>
<input type="button" name="myBut" value="+1" onClick="plus(form,myText)">
<input type="button" name="myBut" value="-1" onClick="minus(form,myText)">



</form>
</body>
</html>

and this is not work
why please ?

Philip M
07-19-2012, 12:58 PM
You must pass the value of the textbox and convert it form a string to a number.


<script type = "text/javascript">

function plus(f,val) {
var count = Number(val);
count += 1;
f.myText.value = count;
}
function minus(f,val) {
var count = Number(val);
count -= 1;
f.myText.value = count;
}

</script>
</head>

<body>
<form>
<input type="text" name="myText" value="0"><br>
<input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
<input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)">
</form>

Quizmaster: In seafood on a restaurant menu, the French word "poisson" translates into English as what?
Contestant: Chicken

yossidd
07-19-2012, 01:03 PM
You must pass the value of the textbox and convert it to a number.



<html>
<head>

<script type = "text/javascript">

function plus(f) {
var count = Number(document.forms[0].myText.value);
count += 1;
document.forms[0].myText.value = count;
}
function minus(f) {
var count = Number(document.forms[0].myText.value);
count -= 1;
document.forms[0].myText.value = count;
}

</script>
</head>

<body>
<form>
<input type="text" name="myText" value="0"><br>
<input type="button" name="myBut" value="+1" onclick="plus(form)">
<input type="button" name="myBut" value="-1" onclick="minus(form)">
</form>


Quizmaster: In seafood on a restaurant menu, the French word "poisson" translates into English as what?
Contestant: Chicken
i or you not understand
i want to do 20 like this input text with +-
if i will send to function like you do i will need 2 funcrion for every input box

i want 20 like this
<input type="text" name="myText" value="1"><br>
<input type="button" name="myBut" value="+1" onClick="plus(form,myText)">
<input type="button" name="myBut" value="-1" onClick="minus(form,myText)">

Philip M
07-19-2012, 01:15 PM
I am not sure that I do understand you. Is this what you want?


<script type = "text/javascript">

function plus(fld) {
var count = Number(fld.value);
count += 1;
fld.value = count;
}
function minus(fld) {
var count = Number(fld.value);
count -= 1;
fld.value = count;
}

</script>
</head>

<body>
<form>
<input type="text" name="myText" value="0"><br>
<input type="button" name="myBut" value="+1" onclick="plus(myText)">
<input type="button" name="myBut" value="-1" onclick="minus(myText)">
<br><br>
<input type="text" name="myText1" value="0"><br>
<input type="button" name="myBut" value="+1" onclick="plus(myText1)">
<input type="button" name="myBut" value="-1" onclick="minus(myText1)">


</form>

yossidd
07-19-2012, 01:17 PM
I am not sure that I do understand you. Is this what you want?


<script type = "text/javascript">

function plus(f,val) {
var count = Number(val);
count += 1;
f.myText.value = count;
}
function minus(f,val) {
var count = Number(val);
count -= 1;
f.myText.value = count;
}

</script>
</head>

<body>
<form>
<input type="text" name="myText" value="0"><br>
<input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
<input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)">
</form>

i want that work




<script type = "text/javascript">

function plus(f,val) {
var count = Number(val);
count += 1;
f.myText.value = count;
}
function minus(f,val) {
var count = Number(val);
count -= 1;
f.myText.value = count;
}

</script>
</head>

<body>
<form>
<input type="text" name="myText" value="0"><br>
<input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
<input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)">

<input type="text" name="myText" value="0"><br>
<input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
<input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)">

<input type="text" name="myText" value="0"><br>
<input type="button" name="myBut" value="+1" onclick="plus(this.form, myText.value)">
<input type="button" name="myBut" value="-1" onclick="minus(this.form, myText.value)">
</form>
and not copy the function 3 times

Philip M
07-19-2012, 02:37 PM
What is wrong with the code I gave you in Post#4 (as revised)?

You cannot have three form fields with the same name myText. You must give them unique names.

Calvert Tripesi
07-19-2012, 05:36 PM
input +2 buttons that make + and -
that work
what it want to do it 20 input like this and i dont want to copy the function
so... i tried to send the name of the text input in the function like this


i want that work


and not copy the function 3 times


<doctype html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type = "text/javascript">

function plusOrMinus( evt )
{
var e = evt || window.event,
elem = evt.target || evt.srcElement,
val = Number( elem.value ),
field;

while( ( field = elem.previousSibling ) && field.type !== 'text' )
elem = field;

if( field )
field.value = Number( field.value ) + val;
}

</script>

</head>

<body>
<form onclick = "plusOrMinus( event )">
<input type="text" name="myText" value="0">
<input type="button" value="+1" >
<input type="button" value="-1" ><br>

<input type="text" name="myText" value="0">
<input type="button" value="+1" >
<input type="button" value="-1" ><br>

<input type="text" name="myText" value="0">
<input type="button" value="+1" >
<input type="button" value="-1" ><br>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum