1

I have a Microsoft Form that I am programmatically filling out with jQuery but after modifying the value in the text box, I can't submit the form even though the text box clearly contains text. The form gives the error, "this question is required," suggesting that the textbox is empty:

I'm trying to simulate whatever event is needed to make the form see the text, but I can't seem to get anything to work, other than to click the textbox and strike a key before it will let me submit the form:

$(document).ready(function () { 
    let questions = null;
    let choices = null;
    let selection = null;
    let textField = null;
    let submitButton = null;

    
    setTimeout(function(){
        // get number of questions on the form
        questions = $(".office-form-question-element");
        console.log("Total number of questions: " + questions.length);

        // choices = radio button options
        choices = questions.eq(0).find(".office-form-question-choice");
        console.log("Total number of choices for question 1: " + choices.length);
        
        // select "option 1"
        selection = choices.eq(0).find(":input");
        selection.eq(0).click(); //simulate mouse click
        
        // populate text for question 2
        textField = questions.eq(1).find(".office-form-textfield").find(":input");
        textField.eq(0).val("Test") //.val() is supposed to trigger object's change event

        // submit button
        submitButton = $(":submit").find(".button-content");
        submitButton.click();
    }, 2000); // wait 2 seconds to allow form to populate to prevent 'null' references
});

I've tried manually triggering the keypress events, several ways, both with and without the ".eq(0): on the textField object, per a similar question posted:

        textField.eq(0).focus();
        textField.eq(0).keydown();
        textField.eq(0).keypress();
        textField.eq(0).keyup();
        textField.eq(0).blur();
        textField.eq(0).change();

This is also supposed to simulate an "enter" key press on a per-character basis, but this didn't work for me either:

        var e = jQuery.Event("keypress");
        e.which = 13; //choose the one you want
        e.keyCode = 13;
        textField.eq(0).trigger(e);
Trent
  • 11
  • 2

0 Answers0