Splitting my function so it runs separately
I'm super new to jQuery and barely grasp the basics of it. I have the following code, which I am embarrassed to say probably took me over 10 hours to create! It sort of works, but it has a small problem.
What it does, is makes some calculations for an employee schedule maker I am working on.
I need the third part (starting on line 40) to happen separately from the rest. Because it uses a couple values obtained from the second part.
My assumption here, is that the two values from lines 46 and 47 are registering as blank, because they are filled out by the earlier part (the ajax call), so I don't actually get the values until the function has completed, so I can't use them there. Is this making sense? lol
I could be wrong about this, I'm not sure.
(I think) I just need the "set up the users hours" (lines 40-58) part to run separately and run immediately after the first part is done. I don't want any extra user clicks or anything to achieve this. But I am so new to jquery, I can't figure out how to do this. I feel like this is something simple, but I'm having trouble nonetheless. Everything I have tried has just butchered it.
Could anyone help me?
- $(".sched_time_s").on('change', function () {
-
- /* ----------------------------------------------------------
- Change input color when filled
- ---------------------------------------------------------- */
-
- if ($(this).val()) {
- $(this).css("background-color","#e9f8d1");
- $(this).css("font-weight","bold");
- }
- else {
- $(this).css("background-color","#fff");
- }
-
- /* ----------------------------------------------------------
- Ajax Request for Conversion
- ---------------------------------------------------------- */
-
- var input = $(this).next();
-
- $.ajax({
-
- url: "ajax_time_convert.php",
- data: "value=" + $(this).val(),
- dataType: 'json',
- success: function(data){
- input.val(data);
- },
- error: function(){
- alert("Error!");
- }
-
- });
-
-
- /* ----------------------------------------------------------
- Sum up the users hours
- ---------------------------------------------------------- */
-
- var weekdayArray = [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ];
- var w;
- for ( w = 0; w < weekdayArray.length; w++ ) {
- for ( var i = 0; i <= <?php echo $last1; ?>; i++ ) {
- var day = weekdayArray[w];
-
- var a = $(".s_convert_" + day + "_" + i).val();
- var b = $(".e_convert_" + day + "_" + i).val();
- var subtract = ( b - a )
- $(".sched_sum_" + day + "_" + i).val(subtract);
-
- var sum = 0;
- $(".sched_sum_day_" + i).each(function(){
- sum += parseInt(this.value);
- $(".weekday_total_" + i).val(sum);
- });
-
- }
- };
-
- });