How to run the same function on different sections on a page with same class name

How to run the same function on different sections on a page with same class name

I have function that is calculating the difference between two fields and showing it in the third field. This is working fine. How can I run the same function if I have multiple "mysection" in my html to calculate the difference between fields within each of these sections?

I have function that is calculating the difference between two fields and showing it in the third field. This is working fine. How can I run the same function if I have multiple "mysection" in my html to calculate the difference between fields within each of these sections?

$(document).ready(function(){
    var total=$(".field2");
    total.keyup(function(){
    var diff = Number($(".field2").val()) - Number($(".field1").val());    
   $(".field3").val(diff.toFixed(2));
    });    
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mysection">
    <input type="text" name="field1" value="" size="30" maxlength="300" class="field1">
    <input type="text" name="field2" value="" size="30" maxlength="300" class="field2">
    <input type="text" name="field3" value="" size="30" maxlength="300" class="field3">
    </div>


javascript jquery

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Understand and Use JavaScript's .forEach() vs. jQuery's .each()

There are two functions to deal with an array on the client-side – JavaScript's .forEach() and jQuery's .each(). Here, I will teach you how to use both of these methods with some sample code.

jQuery vs Vanilla JavaScript - Beau teaches JavaScript

When should you use jQuery instead of vanilla JavaScript? Is jQuery still relevant? What is jQuery good for? jQuery vs vanilla JavaScript - Beau teaches JavaScript

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

jQuery Live -1 | jQuery AJAX Tutorial for Beginners | jQuery Tutorial

jQuery Live -1 | jQuery AJAX Tutorial for Beginners | jQuery Tutorial

Hire Dedicated Jquery Developer

Are You Looking To Hire a jQuery Programmer? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, a leading jQuery application development company, can help you build interactive front-end solutions to leapfrog the digital...