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 ?

Member Avatar for diafol
<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

<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)

<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

<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 ?

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.