Reputation: 15
I have the following jQuery that triggers a function on keyup.
$(document).ready(function() {
$("#calc").on("keyup", "input[type='text']", function() {
calculateSum(this);
});
calculateSum();
});
function calculateSum found here: http://jsfiddle.net/pmetzger/ANp8u/2/
If I load a value into the input field as a default value, obviously the calculateSum will not trigger because it's not a keyup.
I do though, need to be able to load a default value into the input fields at times and I need the calculateSum function to trigger on load in case there is a value that needs calculating. I have added the calculateSum() function at the end of my document.ready but it will only load the global sub-total. I need it to run as if I'm keying up in the value field. I also tried triggering the event, but no luck there as well.
I have put some default values in there for testing.
Upvotes: 0
Views: 86
Reputation: 55740
Try this
var $elem = $('input[type="text"]', "#calc");
$elem.each(function($input) {
if($input.val()) {
$input.trigger('keyup');
}
});
Upvotes: 1
Reputation: 49919
You can do this to trigger the bind event on the first element, you don't need to trigger it on all elements because then it will be run X elements.
$(document).ready(function() {
$("#calc").on("keyup", "input[type='text']", function() {
calculateSum(this);
});
$("input[type='text']").trigger("keyup");
});
Upvotes: 0
Reputation: 12730
You need to either call trigger on the inputs or run the function on them manually like so:
$(document).ready(function() {
//Bind the event
$("#calc").on("keyup", "input[type='text']", function() {
calculateSum(this);
}).find("input[type='text']").each(function() {
calculateSum(this);
});
});
Upvotes: 0