What is preventing my prototype from running?

Whenever the prototype for goingOutChecker() works correctly and I call it within the GeneralChecker() function, I want the Success! and Oops! banners to display at the top of the browser. However, if I comment out the goingOutChecker() prototype and function call, the

monthlyBillCheckerAndSalaryChecker()
prototype and its call work flawlessly. Essentially, both banners should appear as expected when the user clicks 'Submit' provided that the first two fields are either filled in correctly or incorrectly.

I'm wondering why the goingOutChecker() prototype doesn't work properly when I call it inside the GeneralChecker() function. In other words, why don't the banners appear when all three fields are filled in correctly or incorrectly?

It's worth noting that no errors are appearing in the console.

Below is a snippet from the gameTime.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>WOMP</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="gameTime.css">
</head>  
<!-- Rest of HTML content goes here... -->

For more details on this issue, check the complete gameTime.js file below:

function GeneralChecker(salary, fixedExpense, variableExpense) {
        var self = this;
        self.salary = salary;
        self.fixedExpense = fixedExpense;
        self.variableExpense = variableExpense;
        self.isSalaryZeroOrLess = function() {
            // Function implementation code goes here...
        }
        // Other functions defined within the GeneralChecker prototype go here...
    }

    // Finally, an instance of the GeneralChecker class is created and initiated upon button click.

Answer №1

When binding the event handler, make sure to retain the object context. You have a couple of options:

document.querySelector("#btnSubmit").addEventListener("click", function() {
  fin.isSalaryZeroOrLess();
});

or you could try this approach:

document.querySelector("#btnSubmit").addEventListener("click", fin.isSalaryZeroOrLess.bind(fin));

The value of this is determined by how the function is invoked, not just how it's created in relation to an object. By passing the reference to the function in your original code, it loses its connection to the fin object.

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 are the best ways to combine, organize, and iterate through JavaScript objects?

When I run a loop, it returns a combination of integer values and objects as shown below: 2 {firstName:"John", lastName:"Doe", age:28, eyeColor:"blue"} 3 {firstName:"Jane", lastName:"Doe", age:22, eyeColor:"brown"} 1 {firstName:"Jack", lastName:"Doe", age ...

Creating a restriction for executing a function only once per user in JavaScript with React

I am working on implementing a like button feature for reviews, where users can like a review but are restricted from liking the same review more than once. const handleHelpfulButtonClick = () => { console.log(review) props.dispatch(incrementi ...

Learn how to dynamically insert an element into an angular scope using a click event

Currently, I am working on a function called onGroundUserClick within the Scope passedScope. The goal is to have a function triggered upon the completion of loading the iframe that will establish ng-click. var $tdName = $("<td/>", { ...

Display the concealed mat-option once all other options have been filtered out

My current task involves dynamically creating multiple <mat-select> elements based on the number of "tag types" retrieved from the backend. These <mat-select> elements are then filled with tag data. Users have the ability to add new "tag types, ...

Conceal a Div Element on a Website

My website's home page features a CSS animation that slides two images with a high z-index off the screen to reveal the content below. I want this animation to only run the first time the page is accessed during a session, and not every time the user ...

Is there a way to deactivate the <script> tag using CSS specifically for media queries?

When designing a website exclusively for desktop usage, I encountered the issue of it not being viewable on mobile devices. I attempted to address this problem by utilizing the code below: script { display: none; pointer-events: none; } Unfortunat ...

What is the method for altering the color of just the text in an anchor link?

Here is the code snippet I’m working with: <a href="#" title="some title"> <span>Do not change color of this text</span> Want to only change this text color </a> I am trying to change the color of the anchor tag text only, w ...

Utilizing the onFocus event in Material UI for a select input: A comprehensive guide

Having trouble adding an onFocus event with a select input in Material UI? When I try to add it, an error occurs. The select input field does not focus properly when using the onFocus event, although it works fine with other types of input. Check out this ...

change the css style with the !important rule to muiStyle

Currently implementing the latest Material-UI library in my project. I am in the process of migrating old CSS files to new MuiStyles. I am converting it within my MuiStyle object using JavaScript as shown below: const muiStyle = { fabStyle: { displ ...

Error: Angular2 RC5 | Router unable to find any matching routes

I am currently encountering an issue with my setup using Angular 2 - RC5 and router 3.0.0 RC1. Despite searching for a solution, I have not been able to find one that resolves the problem. Within my component structure, I have a "BasicContentComponent" whi ...

When applying json_encode to the PHP $_GET array, it returns a singular string instead of an array containing strings

I am attempting to utilize the PHP GET method in order to generate an array of keywords from a keywordBox field on a different page. For instance, here is how the keywords are added to the page URL: /searchResults.php?keywordBox=computing+finance Althou ...

Error: Attempting to update the value of 'ordersToDisplay' before it has been initialized in a re-render of React. This results in an Uncaught ReferenceError

Trying to dynamically update the document title to include the order number by clicking a button to display different numbers of orders from an array on the screen. The process involves importing a JSON file, filtering it based on user input, calculating ...

I'm having trouble with certain JavaScript functions on my website - some are working fine, but others aren't. Any suggestions on what I should do

I just finished developing a calculator using HTML, CSS, and Javascript. It works flawlessly for all numbers 1 through 9 and the operators +, -, *, /, and %. However, I am facing issues with the number 0, C, and = buttons not functioning properly. Upon in ...

Unexpected outcome: POST data not returning as anticipated

My current setup involves a form (specifically an invoice) that includes a series of input fields for various charges. <tr> <td><label>Description:</label></td> <td><input type="text name="description[]" valu ...

Include a proximity button onto the tabs

Currently, I'm diving into learning Bootstrap and one thing I'd like to implement is having an x next to each tab name for easy closing: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__ ...

Having difficulty scrolling down in a section with 100% height on iOS devices

Currently facing an issue with a website I am creating for my wedding invitation. The top section is set to have a 100% height and requires scrolling to view the rest of the content. While it functions perfectly on FireFox / Chrome on my computer, there s ...

What is the best way to incorporate a horizontal scroll bar that remains fixed at the bottom of the screen?

Within my main container, I have a div that has a height fitting perfectly into the main container but a width wider than the main container with an overflow for the horizontal scroll bar. The challenge that I am facing is: 1. Instead of requiring the use ...

Personalized form outlines

I'm designing a Valentine's Day theme for my website and I was wondering if it's possible to create custom form borders, like a heart shape, without having to hard-code everything. I could always insert an image where needed, but that seems ...

Develop a Vue component for a fixed sidebar navigation

I am in need of a vertical navigation bar positioned to the left of my app's layout, with the content extending across the right side. However, I am currently facing an issue where the navbar is pushing all the content downwards. How can I resolve thi ...

Unable to utilize a custom function within JQuery

I'm facing an issue with using the function I created. The codes are provided below and I keep encountering a "not a function error." var adjustTransparency = function () { //defining the function if ($(this).css('opacity&apo ...