1

I am trying to learn how to automate the input values in some forms when they get repetitive (asking for age, gender, location).

I have investigated the HTML and noticed there are also a few hidden bits (which I unhidden for the screenshot below):

enter image description here

I have never worked with KnockoutJS before and it is not as simple as previous examples I worked on.

For the time being, I managed to create the following for 1 question only:

document.getElementById("sq_101i").value = "26";

 document.addEventListener("DOMContentLoaded", function() {
    document.getElementById("sq_101i").value = "26";
});

But when I do this for the radio buttons, it does not work:

var radioButtons = document.querySelectorAll('.sv_q_radiogroup_control_item');

for (var i = 0; i < radioButtons.length; i++) {
    if (radioButtons[i].value == "Male") {
        radioButtons[i].checked = true;
        break;
    }
}

document.addEventListener("DOMContentLoaded", function() {
            var radioButtons = document.querySelectorAll('.sv_q_radiogroup_control_item');

            for (var i = 0; i < radioButtons.length; i++) {
                if (radioButtons[i].value == "Male") {
                    radioButtons[i].checked = true;
                    break;
                }
            }
        });

Even if I try for the specific question:

var radioButton = document.querySelectorAll('#sq_101i > .sv_q_radiogroup_control_item');

for (var i = 0; i < radioButton.length; i++) {
    if (radioButton[i].value == "Male") {
        radioButton[i].checked = true;
        break;
    }
}

document.addEventListener("DOMContentLoaded", function() {
            var radioButton = document.querySelectorAll('#sq_101i > .sv_q_radiogroup_control_item');

            for (var i = 0; i < radioButton.length; i++) {
                if (radioButton[i].value == "Male") {
                    radioButton[i].checked = true;
                    break;
                }
            }
        });

The outerHTML of one specific radio button is pasted below:

<div data-bind="css: question.getItemClass(item)" class="sv_q_radiogroup sv-q-col-1">
            <label data-bind="css: question.koCss().label" class="sv_q_radiogroup_label">
                <input type="radio" data-bind="attr: {name: question.name + '_' + question.id, value: item.value, id: ($index() == 0) ? question.inputId : '', 'aria-label': question.locTitle.renderedHtml}, checked: question.koValue, enable: !question.koIsReadOnly(), css: question.koCss().itemControl" name="question25_sq_100" value="item1" id="sq_100i" aria-label="1. With what gender do you identify?" class="sv_q_radiogroup_control_item">
                <span data-bind="css: question.koCss().materialDecorator" class="circle"></span>
                <span class="check"></span>
    <span style="position: static;" data-bind="text: koRenderedHtml">Male</span>
                </span>
            </label>
        </div>

My target is to automate the whole thing. To have one code for all the checkboxes with multiple IF statements and then do something like this to automatically answer if the question contains a keyword (e.g. age, name etc):

const myself = { Name: "Doctor Who", Age: "26"};

for (const [key, value] of Object.entries(myself)) {
  const target = [...h3].find(el => el.textContent.includes(key));
  if (target) {
    target.nextElementSibling.value = value;
  }
}
1
  • you are mixing classic javascript and using KnockoutJS. I suggest you use a component for questions rather than creating complex JavaScript.
    – Martlark
    Commented Mar 4 at 22:54

0