Posted: Tue Feb 26, 2008 3:03 pm Summing values from table cells
I'm a first time poster in this forum.
I create very basic html forms for a work related function. Can someone get me started on a method for summing the input values from a table row? A very basic example might look like this:
<tr>
<td>Input 1<input type="text" name="input1"></td>
<td>Input 2<input type="text" name="input2"></td>
<td>Sum <???></td>
Is there a simple javascript that will do this?
Posted: Sun Mar 16, 2008 2:12 am javascript & forms
First thing, get rid of that table stuff. The only thing that should go in tables is tabular data (why? there are quite a few pages that state why).
Second thing to know: how to access the form in javascript. You can access all the forms in a document in javascript via 'document.forms', which is indexed both by number and form name. You can access a form from within the event handler for an input element via 'this.form'. Example:
Code:
<form name="increment">
<button onclick="this.form.out.value=parseInt(this.form.out.value)+1">Increment</button>
<input name="out" value="0">
</form>
<script type="text/javascript">
/* both of the following access the value of the "out" input,
assuming "increment" is the first form in this document
*/
document.forms.increment.out.value;
document.forms[0].out.value;
</script>
Notice input elements are accessed using their name as a property of their parent (the text input with name "out" is accessed via "this.form.out"). To get at the value, you need to access the "value" property of the input element (e.g. "this.form.out.value").
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum