0

I'm trying to automate the adding of a list entry in sharepoint online lists with modern UI and plain JS. Setting the value works fine using:

// Stolen from StackOverflow ;)

const inputTypes = [
    window.HTMLInputElement,
    window.HTMLSelectElement,
    window.HTMLTextAreaElement,
];

const triggerInputChange = (node, value = '') => {

    // only process the change on elements we know have a value setter in their constructor
    if ( inputTypes.indexOf(node.__proto__.constructor) >-1 ) {
        const setValue = Object.getOwnPropertyDescriptor(node.__proto__, 'value').set;
        setValue.call(node, value);
        node.dispatchEvent(new Event('input', { bubbles: true }));
        node.dispatchEvent(new Event('change', { bubbles: true }));
    }
};


var elem;
elem = document.querySelector('input[aria-label="Required Filed „Title“, empty, Field editor. "]');
if(elem) {
   triggerInputChange(elem, "Hello World!");
}

Sadly if you try to submit the form the validation fails with "Field can not be empty" (even though it is set!) I tried to raise every event I could think of but with no success.

By clicking on safe the browser jumps back to that field, if you click save again it validates correctly without having done any change. O_o

Does anyone know how to input a value into a lists form and trigger the validation/input events correctly?

Thank you in advance! :)

2
  • Where are you adding this JavaScript, on modern site page/list form? How you are adding this JavaScript to modern experience, using any custom web part? Commented Apr 22, 2023 at 9:19
  • I'm adding it via an extension - it reads the data from another tab and transfers it to this list. I also tried to insert it into the dev console directly - but that did not do the trick either. :( Commented Apr 24, 2023 at 13:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.