$(document).ready(function () {
let index = 1;
let activeSection = null;
function addOption(type, container) {
let optionHtml;
if (type === "multiple-choice" || type === "checkboxes") {
optionHtml = `
×
`;
} else if (type === "dropdown") {
optionHtml = `
×
`;
}
container.append(optionHtml);
}
function createFormSection() {
let newSection = `
`;
$("#form-container").append(newSection);
index++;
positionAddSectionButton();
}
function positionAddSectionButton() {
if (activeSection) {
let position = activeSection.position();
let buttonWidth = $("#add-section-btn").outerWidth();
let buttonHeight = $("#add-section-btn").outerHeight();
$("#add-section-btn").css({
position: "absolute",
left: position.left - buttonWidth - 47 + "px",
top:
position.top + activeSection.height() / 2 - buttonHeight / 2 + "px",
});
}
}
$("#add-section-btn").on("click", function () {
createFormSection();
$(".form-section").removeClass("active");
activeSection = $(".form-section").last();
activeSection.addClass("active");
positionAddSectionButton();
});
$(document).on("change", ".custom-select", function () {
let type = $(this).val();
let container = $(this).closest(".form-section").find(".options-container");
container.empty();
$(this).closest(".form-section").find(".add-option-btn").remove();
if (type === "short-answer") {
container.append(
' ',
);
} else if (type === "paragraph") {
container.append(
'',
);
} else {
addOption(type, container);
$(this)
.closest(".form-section")
.append(
'Add Option ',
);
}
});
});