The input value in the HTML form was altered momentarily before reverting back to its original state

Researching this topic was quite challenging, but keep reading to find out why.

My objective is to detect any changes in a form field so that I can enable the "Save" button. While this seems easy enough, there's a catch. If the user reverts the input back to its original value when the form initially loaded, I want to disable the save button once again.

Here's a simple example:

  • Page loads - input displays "red"
  • User modifies it to "blue"
  • The script identifies the change from the original value and enables the save button
  • User then decides they preferred "red" and changes the input back to "red"
  • The script recognizes that the change is back to the original value and disables the save button

I understand that this may seem meticulous, but my clients fail to see the necessity of saving the form if the input returns to its initial state.

Naturally, this functionality needs to be applicable to multiple inputs on the form.

Answer №1

It seems like you are utilizing an onChange callback that is linked to the 'change' event for input fields. To ensure that the save button is disabled when the value returns to its original state, you need a reference to the initial value.

The question arises about when you first determine the original value. Presumably, it is when you populate the input field with previously saved data, providing information about the "original" value.

To create specialized onChange callbacks for individual input fields, each with access to the original value, consider employing higher order functions. These functions can return new functions tailored to specific input elements.

function createOnChangeCallback(originalValue) {
   // returning a function!
   return function(changeEvent) {
      // this callback pertains to one input element, with access to "originalValue"
      const newValue = changeEvent.target.value;
      const hasGoneBackToOriginalValue = newValue == originalValue;
      if(hasGoneBackToOriginalValue) {
         // disable the submit button
      }
   }
};

When populating a particular field, follow steps like:

// obtain the input element's reference
const $myInput = $('#myInput');
// assign the initial value to the input field ("populate")
$myInput.val(originalValue);
// generate a specialized callback with access to the original value
$myInput.click( createOnChangeCallback(originalValue) )

Answer №2

Providing some additional context, the application is constructed using the .Net and MVC technology.

The main view page, where most of the action takes place, features a table displaying job request records. Each record includes an "Edit" link as shown:

<a href="/Job/_JobEdit?id=10079" class="btnJobEdit">Edit</a>

To handle events effectively due to dynamic loading and rendering of the table using DataTables.net, I utilize jquery at the document level.

In order to capture the "Click" event of the ".btnJobEdit" links and the "Change" and "Submit" events of the "#frmJobEdit", which only appears upon clicking the above "Edit" link, I employ the following approach.

$(document)
  .on('click', '.btnEditJob', event_click_btnEditJob)
  .on('change', '#frmJobEdit', function () {
    // leveraging jQuery validation library
    $(this).find(':submit').attr('disabled', !$(this).valid()); 
    $(this).find(':reset').attr('disabled', false);
  })
  .on('submit', '#frmJobEdit', event_submit_frmJobEdit)

With the initial page loaded successfully,

Upon clicking the "Edit" button for a record, the function event_click_btnEditJob() is executed.

Here's the breakdown of that straightforward function:

function event_click_btnEditJob() {
  event.preventDefault();

  $.get($(this).attr('href'), function (response) { 
    // expecting a full html form in response from the _JobEdit method of the JobController

    // Setting this into a BootStrap modal dialog and displaying it
    $('#modal_JobEdit .modal-body')
      .html(response)
      .modal('show') 
  }); 
}

When the user hits the "Save Changes" button, the function event_submit_frmJobEdit manages the submission through an ajax call to the JobController. This results in receiving another partial view with the updated data ready for editing again, along with an indication of the submit outcome - success or failure.

The current process is considered efficient, reliable, and straightforward. The introduction of the requirement to toggle the "Save" button based on user edits is seen as unfavorable from a development standpoint. As of now, the customer representatives involved in this project have reached a consensus: "Train the users!"

Answer №3

Here's a suggestion:

$("#your-client-form").submit(function(e){
    e.preventDefault(); // This will stop the form from reloading the page.
});

Answer №4

Here is an illustration of a scenario similar to what you're aiming for.

// Example showcasing how to achieve the desired functionality
// data-field-name attribute specifies the JSON field name
const getFormData = () => {
  return $("#my-form");
};

const getFormStateData = (formData) => {
  let initialFormState = {};
  formData.children(":input").each(function() {
    const fieldName = $(this).data("fieldName");
    const fieldValue = $(this).val();
    if (fieldName) {
      initialFormState[fieldName] = fieldValue;
    }
  });
  return initialFormState;
};

const setSubmitButton = (formData, hasChanged) => {
  formData.children("#submit-form").prop("disabled", hasChanged);
};

const verifyAndUpdateFormState = (formData, initialFormState) => {
  const currentFormState = getFormStateData(formData);
  let hasChanged = false;
  for (const field of Object.keys(initialFormState)) {
    if (initialFormState[field] !== currentFormState[field]) {
      hasChanged = true;
      break;
    }
  }
  setSubmitButton(formData, hasChanged);
};

const attachListenersToFormFields = (formData, initialFormState) => {
  formData.children(":input").each(function() {
    if ($(this).data("fieldName")) {
      $(this).change(function() {
        const fieldName = $(this).data("fieldName");
        const fieldValue = $(this).val();
        verifyAndUpdateFormState(getFormData(), initialFormState);
      });
    }
  });
};

$(document).ready(function() {
  const formData = getFormData();
  attachListenersToFormFields(formData, getFormStateData(formData));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="my-form">
  <input id="name" data-field-name="name" type="text" value="John" />
  <input id="age" data-field-name="age" type="text" value="12" />
  <input id="submit-form" type="submit" />
</form>

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

Rotate the image as you hover your cursor over it

I am trying to implement a script where the image is rotated when the user hovers over it. Unfortunately, the hovering effect is not functioning as expected. $("#alcazar-image").rotate({ bind: { mouseover : function() { $(this).rotate({anima ...

Allow users to zoom in and out on a specific section of the website similar to how it works on Google Maps

I am looking to implement a feature on my website similar to Google Maps. I want the top bar and side bars to remain fixed regardless of scrolling, whether using the normal scroll wheel or CTRL + scroll wheel. However, I would like the central part of the ...

`Erase content from a field once text is typed into a different field`

I have a Currency Converter with two input fields and a button. I enter the amount to be converted in the first field, and the result of the conversion appears in the second field. My question is: How can I automatically clear the second field when I type ...

Is there a way to configure json-server, when utilized as a module, to introduce delays in its responses

json-server provides a convenient way to introduce delays in responses through the command line: json-server --port 4000 --delay 1000 db.json However, when attempting to achieve the same delayed response using json-server as a module, the following code ...

Experiencing Setbacks while Implementing AJAX in a PHP Object Orient

I'm currently attempting to create an object-oriented programming (OOP) login system using Ajax. However, I am encountering issues with starting the session or being redirected to the "Directivo.php" page. When I run the code, there is no output displ ...

Tips for preventing the need to convert dates to strings when receiving an object from a web API

I am facing an issue with a class: export class TestClass { paymentDate: Date; } Whenever I retrieve an object of this class from a server API, the paymentDate field comes as a string instead of a Date object. This prevents me from calling the ...

Oops! The provided value for the argument "value" is not a valid query constraint. Firestore does not allow the use of "undefined" as a value

I encountered an error while exporting modules from file A and importing them into file B. When running file B, the error related to Firebase Cloud Firestore is displayed. const getMailEvents = (startTime, endTime) => { serverRef = db.collection("Ma ...

Using jQuery, you can easily insert a <span> tag around selected text and then save this modification permanently in a local HTML file

I have compiled notes in an HTML file stored on my local computer, with the intention of keeping it solely for personal use. For instance, I have a snippet like this: <p> this is an example text</p> My goal is to highlight the word "example" ...

"Encountered a problem when trying to access file with node

An error has occurred: module.js:471 throw err; ^ Error: Module not found at '/Users/vinclo/app.js' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.runMain (module.js:604:10) at run ( ...

What is the best way to extract the value from a Material UI Slider for utilization?

I am looking to capture the value of the slider's onDragStop event and store it as a const so that I can use it in various parts of my code. However, I am unsure about how to properly declare my const sliderValue and update it. Any guidance on where a ...

Implementing ajax functionality to dynamically insert various user inputs upon a button click

I'm searching for resources or materials that explain how to use jQuery and AJAX to create a feature where multiple inputs can be added to a form with just one click. Google's Gmail client is a perfect example of this functionality, as it enables ...

Switching between different elements in an array using React

I've got a collection of appointments and I need to create a React view that will show them one by one. Users should be able to navigate through the appointments using arrow buttons. Here's an example of what the data looks like: const arr = [ ...

Step-by-step guide for building and populating a JavaScript Dictionary

Does anyone know how to create a Dictionary with a key and a list of values pair in Python? I have been able to create dictionaries with a single value for each key, but now I need to store multiple items as values for each key. Here is what I have tried: ...

Populate an array using a callback function within an async.series operation

I have a callback function within 'async.series' that generates multiple values and creates various outputs from 'elements'. Is there a way to save these return values into an array using 'forEach'? async.series( { ...

In order to enable automatic playback of background images

Having created a slider with hover functionality on icons to change background images, I now seek to add an autoplay feature to the slider. The slider was implemented in a WordPress project using Elementor and involved custom Slider creation through JavaSc ...

c# simulating button click through injected JavaScript

Greetings, I'm seeking assistance in replicating a specific button click on a website. Here is the code for the button: <button type="button" class="btn btn-link btn-xs" onclick="getComponents('188855', '5a0f44a9d70380.12406536&apo ...

What is the best way to loop through an array and apply classes to each element separately?

I'm currently working on a unique jQuery plugin that is designed to create dynamic lists based on any data provided. The first 6 items in the list will always remain constant, regardless of the input data. When I say constant, I mean that although th ...

Leverage the package.json script without relying on any yarn/npm commands

Can scripts commands be executed within the package.json file without needing to include yarn or npm? For example: "scripts": { "get": "node index.js" } Currently, in order to run this script I have to use yarn get [ar ...

Obtain the parent element using the specified id

I currently have a table with the following structure: <tr> <td id="id">2</td> <td id="type"><span class="label">snippets</span></td> <td id="name">all</td> ...

What is the best method for updating audio from a source in Vue.js?

Forgive me if this is a silly question, but I'm still learning the ropes here. I may be going about this in all the wrong ways! I created a backend that learns from a text file and generates sentences along with an audio version of those sentences. I ...