user1426583
user1426583

Reputation: 319

calulating javascript event on keyup

the following works in Jfiddle-but when i use it in original form, the total_full and total_half fields are calculated automatically from other fields, and therefore do not have a keyup or keydown event? therefore the "sub_total" field is not calculating when the "total_full" and total_half fields are updated?

HTML

<td>Total Cost Full Day</td>
<input type="text" name="total_full" id="total_full"  />

<td>&nbsp;</td>
    <td>&nbsp;</td>

 <td>Total Cost Half Day</td>
<input type="text" name="total_half" id="total_half"  />

<td>&nbsp;</td>
    <td>&nbsp;</td>

JAVASCRIPT

//Calculate Sub Total


function calculateSubTotal() {

    var SubTotal = +document.getElementById("total_full").value + +document.getElementById("total_half").value + +document.getElementById("add_on").value;

    document.getElementById("sub_total").value = isNaN(SubTotal) ? 0 : SubTotal;
}


document.getElementById("add_on").onchange = calculateSubTotal;
document.getElementById("add_on").onkeyup = calculateSubTotal;
document.getElementById("total_full").onchange = calculateSubTotal;
document.getElementById("total_full").onkeyup = calculateSubTotal;
document.getElementById("total_half").onchange = calculateSubTotal;
document.getElementById("total_half").onkeyup = calculateSubTotal;
calculateSubTotal;
document.getElementById("total_half").onchange = calculateSubTotal;
document.getElementById("total_half").onkeyup = calculateSubTotal;

http://jsfiddle.net/newbie123/ue62p/5/

Upvotes: 0

Views: 137

Answers (1)

user1426583
user1426583

Reputation: 319

onChange only fires when a user updates the value. If you want that functionality, you're going to have to add calculateSubTotal to the events where total_full and total_half are updated.

Upvotes: 1

Related Questions