Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
refactor form data collection
Browse files Browse the repository at this point in the history
 🐿 v2.12.4
  • Loading branch information
magsallen committed Aug 22, 2019
1 parent 32fbea3 commit 7088db2
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,27 @@ function validate (block){
function generateResponse (overlay){
const context = overlay.content;
const form = document.querySelector('.n-feedback__survey__wrapper-form', context);
const formInputs = form.getElementsByTagName('input');
const formTextArea = form.getElementsByTagName('textArea');
const questionID = formInputs.item(1).name;
const textFieldId = formTextArea.item(0).name;
const textFieldValue = formTextArea.item(0).value;
let formSurveyId;
let formChecked;

for (let i = 0; i < formInputs.length; i++) {
if (formInputs[i].name === 'surveyId') { formSurveyId = formInputs[i].value; }
if (formInputs[i].checked === true) { formChecked = formInputs[i].value; }
}
const data = {};

const response = {
surveyId: formSurveyId,
[questionID]: formChecked,
[textFieldId]: textFieldValue
};
form.querySelectorAll('input,textarea').forEach((element) => {
if (element.type === 'radio') {
if (element.checked) {
data[element.name] = element.value;
}
}

else if (element.type === 'checkbox') {
if (element.checked) {
data[element.name] = (data[element.name] || []).concat(element.value);
}
}

else {
data[element.name] = element.value;
}
});

return response;
return data;
}

function toggleOverlay (overlay){
Expand Down

0 comments on commit 7088db2

Please sign in to comment.