Javascript Calculations and Sums

Imagine having multiple fields for inputting loan totals, let's call them loan1 and loan2 for simplicity.

window.addEvent('domready', function() {
$('loan1').addEvent('change', calculateSubtotal);
$('loan2').addEvent('change', calculateSubtotal);

});
function calculateSubtotal(){
$('subtotal').value = Number($('loan1').value) + Number($('loan2').value) ;
}

After calculating the subtotal of all loans entered, I now aim to generate a grand total by taking 1% of the subtotal and adding 295. This is where I need assistance in converting it into JavaScript.

So the formula stands as follows: loan1 + loan2 = subtotal

(subtotal*.01)+295=Grandtotal

Your guidance on this matter is greatly appreciated!

Answer №1

If you have a field identified by the id grandtotal, you can easily update your rekenen1 function by adding the following line:

function rekenen1(){
    var subtotal = Number($('#loan1').val()) + Number($('#loan2').val());
    $('#subtotal').val(subtotal);
    $('#grandtotal').val(subtotal*0.01+295);
}

Additionally, remember to update the addEvent functions accordingly:

$().ready(function() {
    $('#loan1').addEvent('change', rekenen1);
    $('#loan2').addEvent('change', rekenen1);

});

I also made changes to use the jquery val function in the rekenen1 function and updated the event triggers.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Several adhesive panels on a dynamic webpage

In the content area of my page, a dynamic number of rows are generated. Each row consists of two columns: a side block and a content area. The goal is to have the side block stick while the page scrolls down until the next block appears and pushes the prev ...

The JavaScript copy function is no longer functioning properly following the update to the href link

I have implemented a function for copying to clipboard as shown below: function CopyToClipboard(containerid) { if (document.selection) { var range = document.body.createTextRange(); range.moveToElementText(document.getElementById(containerid)); ...

Creating a checkbox with AngularJS

My attempt at creating a checkBox using AngularJS led me to this code snippet: http://jsfiddle.net/t7kr8/211/. After following the steps outlined in the code, my implementation looked like this: JS File: 'use strict'; var app = angular.module ...

In what manner does the Date class's constructor modify the unary + operator?

While browsing through this answer regarding the Hidden Features of JavaScript?, I came across a perplexing behavior: > new Date < Fri Feb 26 2016 21:15:43 GMT-0500 (EST) > +new Date < 1456539382581 < 0 + new Date + 0 "0Fri Feb 26 2016 21:1 ...

Adding an image within the body of text in a Django model, where both the text and image coexist

I am currently seeking a method to seamlessly insert an image within the text of my Django-powered blog. My goal is to achieve a layout similar to the one showcased in this example: https://i.stack.imgur.com/cFKgG.png The desired layout consists of two c ...

When the CSS class of a DIV is set to X in jQuery, the script will hide all other DIVs with a

Just starting to explore jQuery and still fresh on JS knowledge, I have the desire to craft a jQuery script. To begin with, here is my HTML snippet: <div id="user-controls"> <div class="choice" id="choice-all" onclick="showAll();">All< ...

Why did the bootstrap installation in my project fail?

Previously, everything on my website was functioning correctly. However, upon launching it now, I noticed that the carousel, buttons, and other elements are broken. It seems like there might be an issue with the Bootstrap CDN as the buttons and sliders are ...

Retrieving the latest entry from the MySQL database

I am encountering an issue with a code that involves fetching data from MySQL into an array. I have a form with color and size select boxes, and when an onclick javascript function is triggered it creates two additional select boxes. I have successfully re ...

I recently started delving into React Native and am currently exploring how to implement custom fonts in my application. However, I have encountered an error that is preventing me from successfully integrating

The Issue: The error I encountered only appeared after including font-related code (such as importing from "expo-font" and using "AppLoading" from "expo", and utilizing the "Font.loadAsync()" function). Error: Element type is invalid: expected a string (fo ...

Effectiveness in identifying groups (?: => task(?:s+)?team COMPARED TO effort(s+)?group

Both of these formats suit my needs: E1=> work(?:\s+)?group E2=> work(\s+)?group I am looking to match either workgroup or work group, taking into account that the space could also be a line break (\s+)? My main concern is with th ...

express-validator: bypass additional validation in a user-defined validator

Utilizing the express-validator package for validating my request data. As per the documentation, we need to declare them in this manner: app.use(expressValidator({ customValidators: { isArray: function(value) { return Array.isArray(value); ...

How can I halt the execution of the setInterval function in jQuery?

In my code, I have a timer function that triggers when it reaches below 1 minute. It then flashes specific text using a JavaScript function. You can see the initial setup in this jsfiddle: http://jsfiddle.net/8yned9f9/5/ $(document).ready(function(){ ...

Does the Error page in Next JS 13 fail to properly manage fetch API errors?

After referencing the documentation for Next.js with app router, I followed the instructions to create an error.tsx page within my app folder (app/[lang]/error.tsx). This file contains the code provided in the docs. Additionally, I included some API calls ...

Combining elements from a single array list and transferring them to another array list using Angular 4

In my arrayList called selectedSources, I have items such as: this.selectedSources.push( { id: 0, text: "A" }, { id: 1, text: "B" }, { id: 2, text: "C" }, { id: 3, text: "D"} ); The user has the option to select one or more of these items. When i ...

I attempted to publish my React application using gh-pages and encountered the following error: "The argument for 'file' must be a string. However, it was received as undefined."

I encountered an issue while attempting to deploy my React application with gh-pages. The error message I'm facing states: "The 'file' argument must be of type string. Received type undefined." Initially, I suspected that the problem was wi ...

Showcasing the time variance using javascript/jquery

I am currently using a datepicker to select dates, with the intention of calculating the difference between the chosen dates and then displaying an alert with the calculated difference. Unfortunately, I am encountering issues with getting the code to work ...

Knockout problem: Unable to utilize the 'in' operator for finding 'length'

I encountered an error with my web application that is using Knockout.js Received the error message: "Cannot use 'in' operator to search for 'length'" This is my Code: $(document).ready(function () { AjaxRequest(); }); ...

Retrieve a form (for verification purposes) from a separate AngularJS component

I'm facing an issue with accessing a form from one component to another in my project. One component contains a form, and the other has navigation buttons that, when clicked, should validate the form before moving on to the next step. However, I alway ...

Incrementally add a new object to an existing array of objects

I have an array of objects below. When I do a console.log, this is what I see: [Object, Object, Object] 0:Object name: "Rick" Contact: "Yes" 1:Object name:"Anjie" Contact:"No" 2:Object name:"dillan" Contact:"Maybe" Now, I wa ...

Tips for adding a gradient to your design instead of a plain solid color

I stumbled upon a snippet on CSS Tricks Attempting to replace the green color with a gradient value, but unfortunately, the value is not being applied. I have tried using both the fill property and gradient color, but neither has been successful. Here is ...