skip to Main Content

I have been using this line of code on an input box successfully for a decade:

var importDate = "03/07/2023";
$("#es-modal-very-important-date").val(importDate).trigger('change')

Today it quit working. It does put the date into the box but it does not trigger the change. So the webpage doesn’t recognize the date. It’s like the box requires a user click or interaction with a datepicker to trigger the change.

I also tried the following to trigger the change without luck:

var myInput = document.getElementById("es-modal-very-important-date");
myInput.dispatchEvent(new Event("change"));

Are there any other options? I can only think the programmers who built the webpage (for adding records) have changed something that made the trigger stop working. I don’t have access to the programmers. I use javascript to automate adding about 100 or so records everyday.

I tried the following:

var importDate = "03/07/2023";
$("#es-modal-very-important-date").val(importDate).trigger('change')

and

var myInput = document.getElementById("es-modal-very-important-date");
myInput.dispatchEvent(new Event("change"));

I expected it to add the importDate into the input box and then to trigger a change so the web page can recognize the change. It does add the importDate but the webpage does not recognioze it.

2

Answers


  1. Using jQuery, how about trying this?

    $('#es-modal-very-important-date').change();
    
    Login or Signup to reply.
  2. In my experience, the only reliable way to set things on basically any webpage (including React apps) is to use Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set, e.g.:

    const set = (selector, value) => {
        const input = document.querySelector(selector);
        const nativeInputValueSetter = 
            Object.getOwnPropertyDescriptor(
                window.HTMLInputElement.prototype,
                "value"
            ).set;
        nativeInputValueSetter.call(input, value);
    }
    
    set('#es-modal-very-important-date', importDate)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search