Populating hidden fields in Javascript with mouseover event trigger - Pre-click action

Seeking a solution for another issue, I stumbled upon the capability to execute JavaScript on mouseover.

Here is what I want to execute:

$(document).ready(function(){

function myPayment()
{

var value = document.mortgagecalc.value_input.value;
var rate = document.mortgagecalc.rate_input.value;
var term = document.mortgagecalc.term_input.value;
var deposit = document.mortgagecalc.deposit_input.value;
var fee = document.mortgagecalc.fee_input.value;
var totalloan = ((value + fee) - deposit) * 1 + rate;
var totalinterest = (totalloan * 1 + rate) - totalloan;
var months = (term * 12);
var repay = totalloan / months;
var interest = totalinterest / months;

// Calculate mortgage payment and display result
document.getElementById('repay_input').value = repay;
document.getElementById('interest_input').value = interest;
}
}

Upon hovering over my submit button, I would like this script to fill in these fields, which will then be included in the form action:

<input type="hidden" name="repay" id="repay_input" value="">
<input type="hidden" name="interest" id="interest_input" value="">

Currently, it does not seem to be working. This could be due to a small mistake or the fact that it may not work at all.

Thank you - If anyone has a better suggestion on how to achieve my goal, please inform me. I have attempted PHP but without success.

Adding a button for clarification:

<button class="col-sm-4 enter-button" type="submit" name="submit" onmouseover="return myPayment()" value="Calculate">Calculate Payments</button>

Update:

I made adjustments to the JavaScript and the button, but forgot to match the form's name with the reference in the JS.

Furthermore, my mortgage calculations were significantly inaccurate due to the way the form collects the percentage rate.

Answer №1

To ensure that your function is declared outside of jQuery's document.ready wrapper, modify your button code to resemble the following:

function calculatePayment()
{
  var value = document.mortgagecalc.value_input.value;
  var rate = document.mortgagecalc.rate_input.value;
  var term = document.mortgagecalc.term_input.value;
  var deposit = document.mortgagecalc.deposit_input.value;
  var fee = document.mortgagecalc.fee_input.value;
  var totalloan = ((value + fee) - deposit) * 1 + rate;
  var totalinterest = (totalloan * 1 + rate) - totalloan;
  var months = (term * 12);
  var repay = totalloan / months;
  var interest = totalinterest / months;

  // Perform calculations and display the results
  document.getElementById('repay_input').value = repay;
  document.getElementById('interest_input').value = interest;
}
<button onmouseover="calculatePayment()" ...></button>

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

What is causing the code behind to reject the href with 'aspx' in the anchor tag?

I am currently working on implementing a calendar control that will display Today's Due and Overdue items in separate accordion sections when a date is selected. To achieve this, I have written the necessary code in the back end and used a style.css f ...

Storing and accessing nested JSONB data in PostgreSQL within a Rails form

Upon realizing the lack of information on utilizing rails forms to store and retrieve JSON data, I encountered a specific issue: Within my form, I am creating a nested structure in JSON: = form_for(@car) do |cf| = cf.fields_for :locations do | locati ...

What is the process for developing a multi-threaded ASP.NET Ajax update panel?

Hey there, I was wondering if you could provide some insights on how to develop a multi-threaded ASP.NET AJAX update panel? Your help would be greatly appreciated! ...

Declarations and expressions in Node.js using JavaScript

Recently diving into Node Js, I stumbled upon the following code snippet in a tutorial const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const questions = [ " ...

What is the best way to create a keyup event for the entire document except for one specific element?

Looking for some advice on how to handle a specific code scenario. Here's what I have: $(document).ready(function(){ $(document).on("keyup",function(e){myFunction(e)}); }); function myFunction(e){ console.log("hi"); } In ...

Why isn't my List<string> being retrieved from the MVC Controller in an $ajax request?

I am attempting to generate customized lists in my cshtml file through an ajax request. .ajax({ type: "GET", cache: false, url: "@Url.Action("getValidationLists")", contentType: "application/json", dataType: "json", ...

What is the best way to remove headers and footers programmatically in print pages on Safari using JavaScript?

Struggling with eliminating the header and footer on print pages in Safari using JavaScript? While disabling the header and footer manually can be done through print settings in most browsers, my aim is to automate this process with code to ensure that use ...

What criteria should I use to determine if a post has been favorited by a user using Mongoose?

Creating a function for users to like posts has been my recent project. Each post is stored as an object in my MongoDB collection with a specific schema. { title: String, text: String } On the other hand, users have their own unique schema as well. ...

Update the configurable product process for the custom attribute 'delivery_time' in Magento 1.9.2

I am currently using Magento 1.9.2.4 (1.9.2.3 in my Testpage) and I have a few configurable products with multiple options. Each product (child of the configurable one) has a different delivery time. To achieve this, I created an attribute called "delivery ...

The code fails to execute within the specified time interval

I am trying to implement a jQuery code that calls every 5 seconds, but unfortunately the code is not working as expected. Here is my current implementation: setInterval($.getJSON('friendshipRequestCount', function(data) { var cnt=data. ...

Customize the month template of the Kendo datepicker calendar popup in an AngularJS application

I've been working on adjusting the month template for an existing calendar popup. Initially, I used the code below: $("#" + attributeId).kendoDatePicker({ dates: jsonResult, weekNumber: true, month: { // template for dates in ...

Determine the total number of arrays present in the JSON data

I'm currently working on a straightforward AngularJS project, and here's the code I have so far: This is my view: <tr ng-repeat="metering in meterings"> <td>1</td> <td>{{metering.d.SerialNumber}}</td> ...

React blank state - State remains undefined after calling setState

I took out the imports because they are not causing any issues When I render, I set my state and it logs correctly in the console. However, when I try to map it, it comes back as null and gives me an error stating that there are no elements in the "allInf ...

V-Calendar is not displaying the accurate dates

https://i.stack.imgur.com/zk4h7.png The image displays dates starting on June 1, 2022, which should be a Wednesday but appears as a Sunday on the calendar. This issue affects all months as they start on a Sunday instead of their respective weekdays. The p ...

The debounced function in a React component not triggering as expected

I am facing an issue with the following React component. Even though the raiseCriteriaChange method is being called, it seems that the line this.props.onCriteriaChange(this.state.criteria) is never reached. Do you have any insights into why this.props.onC ...

Retrieving a universal variable within AngularJS

Is there a way to initialize an Angular model using a JSON object that is embedded within an HTML page? Take this as an example: <html> <body> <script type="text/javascript" charset="utf-8"> var tags = [{&q ...

What is the best way to replicate a synchronous ajax call? (mimicking synchronous behavior with asynchronous methods)

Given that a "native" synchronous ajax call can block the user interface of the browser, it may not be suitable for many real-world scenarios (including mine). I am curious to know if there is a way to mimic a synchronous (blocking) ajax call using an asy ...

Using jQuery Ajax and PHP for Secure User Authentication

Hello everyone, I could really use some assistance right now. The issue I'm facing is that the error div is not showing the content I expect in the success function of my AJAX request. I've tried alerting the response, which returns true if the ...

What is the best way to use multiple encoding types when sending data via a POST method in Node.js?

My current challenge involves sending a combination of name and description text data alongside a video file. Unfortunately, I've encountered an issue where I can only send either the video or the text, but not both simultaneously. Below is the code ...

Removing outline from a Material UI button can be done using a breakpoint

Is there a way to remove the outlined variant for small, medium, and down breakpoints? I have attempted the following approach: const selectedVariant = theme.breakpoints.down('md') ? '' : 'outlined'; <Button name="buy ...