From 3f0e4c57b60f1ec1e468a60e7c3f48d53cd316d7 Mon Sep 17 00:00:00 2001 From: matt <10769515+whosmatt@users.noreply.github.com> Date: Tue, 1 Aug 2023 23:40:37 +0200 Subject: [PATCH] added addRadioButton helper function --- js/modframework.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/js/modframework.js b/js/modframework.js index b73c81a..11746ab 100644 --- a/js/modframework.js +++ b/js/modframework.js @@ -224,4 +224,36 @@ function addInputField(parentDiv, labelText, defaultValue) { parentDiv.appendChild(formGroup); return input; // Return the input element +} + +/** + * Adds a radio input field to a parent div with an id, name, value and label. + * + * @param {HTMLElement} parentDiv - The parent div to which the input field will be added. Usually this.modSpecificDiv + * @param {string} labelText - The label text (title) for the input field. + * @param {string} id - The id is needed to link radio button and label, choose any unique id. + * @param {string} name - The name of the radio button needs to be the same for all radio buttons in a mutually exclusive group. + * @returns {HTMLInputElement} - The created input element, assign it to a constant for later use. + */ +function addRadioButton(parentDiv, labelText, id, name) { + const formCheckDiv = document.createElement("div"); + formCheckDiv.classList.add("form-check"); + + const inputElement = document.createElement("input"); + inputElement.classList.add("form-check-input"); + inputElement.type = "radio"; + inputElement.name = name; + inputElement.id = id; + + const labelElement = document.createElement("label"); + labelElement.classList.add("form-check-label"); + labelElement.htmlFor = id; + labelElement.textContent = labelText; + + formCheckDiv.appendChild(inputElement); + formCheckDiv.appendChild(labelElement); + + parentDiv.appendChild(formCheckDiv); + + return inputElement; } \ No newline at end of file