i want to re-calculate everything not only on change, but also when the page loads.
i tried the following:
$(document).ready(function () {
//trigger misc costs
$("input[class~='miscc']").trigger('change');
$("input[class~='misch']").trigger('change');
//other events here....
//add up repeater miscaleneous mnumbers
//dollar amoungs
$("input[class~='miscc']").change(function (event) {
//only allow numbers
this.value = this.value.replace(/[^0-9\.]/g, '');
var sum = 0;
var num = 0;
$("input[class~='miscc']").each(function (event) {
num = parseFloat($(this).val()) || 0;
sum = sum + num;
});
$("input[id*='txtMiscC']").val(sum.toFixed(2));
$("input[class~='cost']").trigger('change');
});
//hours
$("input[class~='misch']").change(function (event) {
//only allow numbers
this.value = this.value.replace(/[^0-9\.]/g, '');
var sum = 0.00;
var num = 0;
$("input[class~='misch']").each(function (event) {
num = parseFloat($(this).val()) || 0;
sum = sum + num;
});
$("input[id*='txtMiscH']").val(sum.toFixed(1));
$("input[class~='hours']").trigger('change');
});
});
but nothing is happening, am i doing it wrong?
i want to re-calculate everything not only on change, but also when the page loads.
i tried the following:
$(document).ready(function () {
//trigger misc costs
$("input[class~='miscc']").trigger('change');
$("input[class~='misch']").trigger('change');
//other events here....
//add up repeater miscaleneous mnumbers
//dollar amoungs
$("input[class~='miscc']").change(function (event) {
//only allow numbers
this.value = this.value.replace(/[^0-9\.]/g, '');
var sum = 0;
var num = 0;
$("input[class~='miscc']").each(function (event) {
num = parseFloat($(this).val()) || 0;
sum = sum + num;
});
$("input[id*='txtMiscC']").val(sum.toFixed(2));
$("input[class~='cost']").trigger('change');
});
//hours
$("input[class~='misch']").change(function (event) {
//only allow numbers
this.value = this.value.replace(/[^0-9\.]/g, '');
var sum = 0.00;
var num = 0;
$("input[class~='misch']").each(function (event) {
num = parseFloat($(this).val()) || 0;
sum = sum + num;
});
$("input[id*='txtMiscH']").val(sum.toFixed(1));
$("input[class~='hours']").trigger('change');
});
});
but nothing is happening, am i doing it wrong?
Share Improve this question asked Dec 3, 2013 at 17:56 Madam Zu ZuMadam Zu Zu 6,61520 gold badges90 silver badges132 bronze badges2 Answers
Reset to default 6You are triggering the event before you bind the event handler. First bind the handler, then trigger the event.
Add change events to the bottom.
$(document).ready(function () {
//other events here....
//add up repeater miscaleneous mnumbers
//dollar amoungs
$("input[class~='miscc']").change(function (event) {
//only allow numbers
this.value = this.value.replace(/[^0-9\.]/g, '');
var sum = 0;
var num = 0;
$("input[class~='miscc']").each(function (event) {
num = parseFloat($(this).val()) || 0;
sum = sum + num;
});
$("input[id*='txtMiscC']").val(sum.toFixed(2));
$("input[class~='cost']").trigger('change');
});
//hours
$("input[class~='misch']").change(function (event) {
//only allow numbers
this.value = this.value.replace(/[^0-9\.]/g, '');
var sum = 0.00;
var num = 0;
$("input[class~='misch']").each(function (event) {
num = parseFloat($(this).val()) || 0;
sum = sum + num;
});
$("input[id*='txtMiscH']").val(sum.toFixed(1));
$("input[class~='hours']").trigger('change');
});
//trigger misc costs
$("input[class~='miscc']").trigger('change');
$("input[class~='misch']").trigger('change');
});