0

I am trying to get a text box to show a number and then add it to another number,

<html>
<body>

Field1: <input type="int" id="field1" value="1" />
<br />
Field2: <input type="int" id="field2" />
<br /><br />
Click the button to add the content of Field1 to Field2.
<br />
<button onclick=" "field2.text" = (field2.text + field1.text).value">Copy Text</button>

</body>
</html>

anyone know how to make this work please ?

2
Contributors
5
Replies
9
Views
7 Years
Discussion Span
Last Post by Kniggles
0
<button onclick="document.getByElementId('field2').value = document.getByElementId('field2').value + document.getByElementId('field1').value;return false;">Copy Text</button>

I think this will just concatenate strings - I may be wrong. If you need to have them numerically added, use the parseFloat() or parseInt() functions around the elements to be added.

http://www.javascripter.net/faq/convert2.htm

Edited by diafol: n/a

0
<FORM NAME = frmOne>

Number One: <INPUT TYPE = Text NAME = txtFirstNumber SIZE = 5 value ="">

Number Two: <INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="">
<P>
Total: <INPUT TYPE = Text NAME = txtThirdNumber SIZE = 5 value = "">
<P>
<Input Type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>

</FORM>
<SCRIPT language = JavaScript>

function calculate() {
A = document.frmOne.txtFirstNumber.value
B = document.frmOne.txtSecondNumber.value
C = (A + B)
document.frmOne.txtThirdNumber.value = C
}

</SCRIPT>

this JOINS box 1 to box 2 and displays in box 3 (1,2)

Edited by Kniggles: Joins not add,

0
<FORM NAME = frmOne>

Number One: <INPUT TYPE = Text NAME = txtFirstNumber SIZE = 5 value ="">

Number Two: <INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="">
<P>
Total: <INPUT TYPE = Text NAME = txtThirdNumber SIZE = 5 value = "">
<P>
<Input Type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>

</FORM>
<SCRIPT language = JavaScript>

function calculate() {
A = document.frmOne.txtFirstNumber.value
B = document.frmOne.txtSecondNumber.value

A = Number(A)
B = Number(B)
C = (A + B)
document.frmOne.txtThirdNumber.value = C
}

</SCRIPT>

this will add them

0
<FORM NAME = frmOne>

Start: <INPUT TYPE = Text NAME = x SIZE = 5 value ="0">

Number Two: <INPUT TYPE = Text NAME = y SIZE = 5 value ="1">
<P>
Total: <INPUT TYPE = Text NAME = z SIZE = 5 value = "">

<P>

<p>
<Input Type = Button NAME = b1 VALUE = "Pluss One" onClick = calculate()>

</FORM>
<SCRIPT language = JavaScript>

function calculate() {
A = document.frmOne.x.value
B = document.frmOne.y.value

A = Number(A)
B = Number(B)
C = (A + B )
document.frmOne.z.value = C
document.frmOne.y.value = C
}

</SCRIPT>

This adds ONE however will only do it once , how do you keep adding one when you click ?

0

SORTTED ...................

<FORM NAME = frmOne>

Number of clicks: <INPUT TYPE = Text NAME = w SIZE = 5 value ="0">

<Input Type = Button NAME = b1 VALUE = "Pluss One" onClick = calculate()>

</FORM>
<SCRIPT language = JavaScript>

function calculate() {

D = document.frmOne.w.value

D = Number(D)

D = (D + 1)

document.frmOne.w.value = D
}

</SCRIPT>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.