Trigger change event?

I’m creating a custom component and all is good except I’m having some problem saving a value back out to a field name.

document.getElementById(“fieldname”).value=inputValue;

and this sucessfully places a calculated field onto the form

on screen but when I click save the field value displayed on the form’s field is not saved so I think I’ve got to trigger a field change event on that field.?

I’ve tried

const autosuggest = document.getElementById(“w3w-map”);
autosuggest.addEventListener(
“coordinates_changed”,
(ev,dt) => {
console.log(ev);
// alert(JSON.stringify(dt));
setTimeout(function(){
const searchInput = document.getElementById(“search-input”);
const inputValue = searchInput.value;
document.getElementById(“fielda6jMnn0r75”).value=inputValue;
},100);
// Get a reference to the input field
var fielda6jMnn0r75 = document.getElementById(“fielda6jMnn0r75”);
// Add an event listener for the change event
fielda6jMnn0r75.addEventListener(“change”, function() {
console.log(“Change event fired on fielda6jMnn0r75”);
});
// Set the new value for the input field
//var newValue = inputValue;
//fielda6jMnn0r75.value = newValue;

// Create a new change event
var changeEvent = new Event(“change”, { bubbles: true });

// Dispatch the change event on the input field
fielda6jMnn0r75.dispatchEvent(changeEvent);
}
);

and I can see the change event firing but still the value is not written to the record.

Can you see if this helps?

You might need to add “.change()” after defining the value in the input field.

1 Like

It was an issue with the timing of the event handlers so the delay worked with the following code:

const autosuggest = document.getElementById(“w3w-map”);

autosuggest.addEventListener(“coordinates_changed”, (ev, dt) => {
console.log(ev);
// Delay the execution to ensure the value is updated after the event
setTimeout(function () {
const searchInput = document.getElementById(“search-input”);
const inputValue = searchInput.value;
// Update the value of the fielda6jMnn0r75 field
document.getElementById(“fielda6jMnn0r75”).value = inputValue;

    // Create and dispatch a change event on the fielda6jMnn0r75 field
    var changeEvent = new Event("change", { bubbles: true });
    document.getElementById("fielda6jMnn0r75").dispatchEvent(changeEvent);
}, 100);

});

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.