Run a jQuery function for each element with a certain class

Adrian

I have a jQuery function which calculates the hours and minutes between two given times (hh:mm format). It's working perfectly fine but I'm trying to apply this function for each item in my HTML structure. Each item is wrapped into a div with class .item

My HTML:

<div class="item">
    <label for="start">Start</label>
    <input class="start" id="start" value="10:00" />

    <label for="end">End</label>
    <input class="end" id="end" value="12:15" />

    <label for="hours">Hours</label>
    <input class="hours" id="hours" value="" readonly />
</div>

<div class="item">
    <label for="start">Start</label>
    <input class="start" id="start" value="10:00" />

    <label for="end">End</label>
    <input class="end" id="end" value="13:30" />

    <label for="hours">Hours</label>
    <input class="hours" id="hours" value="" readonly />
</div>

<button id="calculate" onclick="calculate()">Calculate</button>

The script:

$(function () {
     function calculate() {
             time1 = $(".start").val().split(':'),
             time2 = $(".end").val().split(':');
             hours1 = parseInt(time1[0], 10), 
             hours2 = parseInt(time2[0], 10),
             mins1 = parseInt(time1[1], 10),
             mins2 = parseInt(time2[1], 10);
             hours = hours2 - hours1,
             mins = 0;
         if(hours < 0) hours = 24 + hours;
         if(mins2 >= mins1) {
             mins = mins2 - mins1;
         } else {
             mins = (mins2 + 60) - mins1;
         }

         // the result
         $(".hours").val(hours + ':' + mins);         
     }

         $(".start,.end").change(calculate);
         calculate();

});

My question is: How can I apply the jQuery .each() function in order to calculate the hours for each item in part? Or there is a better approach for this?

JSFiddle: http://jsfiddle.net/44NCk/8/

Thanks!

Malk

Loop through all items in the calculate function.

$(function () {
     function calculate() {
         $(".item").each(function(){
             var $start = $(this).find(".start"),
                 $end = $(this).find(".end"),
                 $result = $(this).find(".hours"),
                 time1 = $start.val().split(':'),
                 time2 = $end.val().split(':'),
                 hours1 = parseInt(time1[0], 10), 
                 hours2 = parseInt(time2[0], 10),
                 mins1 = parseInt(time1[1], 10),
                 mins2 = parseInt(time2[1], 10);
                 hours = hours2 - hours1,
                 mins = 0;

             if(hours < 0) hours = 24 + hours;
             if(mins2 >= mins1) {
                 mins = mins2 - mins1;
             } else {
                 mins = (mins2 + 60) - mins1;
             }

             // the result
             $result.val(hours + ':' + mins);   
         });
     }
    $(".start,.end").on("change", calculate);
    $("#calculate").on("click", calculate);
    calculate();

});

http://jsfiddle.net/8MfCr/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Run jQuery function for each element separately

From Dev

Run jQuery function for each element separately

From Dev

jQuery: Run function if each tr has class

From Dev

Run function for each element separately?

From Dev

jquery element in viewport + each function

From Dev

hide all but one element of certain class in Jquery

From Dev

jQuery: selecting an element having a certain id and a certain class, both as variables

From Dev

jQuery checking each element against each other element in a class

From Dev

jQuery: Iterating through each element with CSS class

From Dev

Use JQuery each function to return element width

From Dev

Find closest element from an Each function in jQuery

From Dev

jQuery each() function control the same element

From Dev

jQuery append() function not appending for each <p> element

From Dev

How to select a class's last element for each occurrence of the class in jQuery

From Dev

How to get index, position number of element with certain class with jQuery

From Dev

jQuery replace all of an element's classes of a certain pattern with a different class

From Dev

Find an element and then find first from it with a certain class and inject an html in jQuery

From Dev

How to remove elements of a certain class inside a parent element Jquery

From Dev

on click toggle for child element of the certain class (handlers) - jquery

From Dev

jQuery each function for all images under certain ul & li tag

From Dev

Run jQuery split() function only if div contains certain text

From Dev

How can I run function for each element in different time?

From Dev

jQuery each: If element is in the element

From Dev

jQuery - Set each html() class value to another element

From Dev

How to position each element with a class identifier differently using jQuery?

From Dev

jQuery count each first li element with class name

From Dev

jQuery append html element but with a different class name each time

From Dev

jquery - add different class for each anchor element when hover

From Dev

JQuery Looping a slice(), split(), and wrapped array...for each element of class

Related Related

  1. 1

    Run jQuery function for each element separately

  2. 2

    Run jQuery function for each element separately

  3. 3

    jQuery: Run function if each tr has class

  4. 4

    Run function for each element separately?

  5. 5

    jquery element in viewport + each function

  6. 6

    hide all but one element of certain class in Jquery

  7. 7

    jQuery: selecting an element having a certain id and a certain class, both as variables

  8. 8

    jQuery checking each element against each other element in a class

  9. 9

    jQuery: Iterating through each element with CSS class

  10. 10

    Use JQuery each function to return element width

  11. 11

    Find closest element from an Each function in jQuery

  12. 12

    jQuery each() function control the same element

  13. 13

    jQuery append() function not appending for each <p> element

  14. 14

    How to select a class's last element for each occurrence of the class in jQuery

  15. 15

    How to get index, position number of element with certain class with jQuery

  16. 16

    jQuery replace all of an element's classes of a certain pattern with a different class

  17. 17

    Find an element and then find first from it with a certain class and inject an html in jQuery

  18. 18

    How to remove elements of a certain class inside a parent element Jquery

  19. 19

    on click toggle for child element of the certain class (handlers) - jquery

  20. 20

    jQuery each function for all images under certain ul & li tag

  21. 21

    Run jQuery split() function only if div contains certain text

  22. 22

    How can I run function for each element in different time?

  23. 23

    jQuery each: If element is in the element

  24. 24

    jQuery - Set each html() class value to another element

  25. 25

    How to position each element with a class identifier differently using jQuery?

  26. 26

    jQuery count each first li element with class name

  27. 27

    jQuery append html element but with a different class name each time

  28. 28

    jquery - add different class for each anchor element when hover

  29. 29

    JQuery Looping a slice(), split(), and wrapped array...for each element of class

HotTag

Archive