1
|
|
|
const baseDomain = document.baseURI; |
2
|
|
|
const submitURL = 'partialuserform'; |
3
|
|
|
const buttons = () => Array.from(document.body.querySelectorAll('form.userform ul li.step-button-wrapper button')); |
4
|
|
|
const formElements = () => Array.from(document.body.querySelectorAll('form.userform [name]')); |
5
|
|
|
|
6
|
|
|
const getElementValue = (element, fieldName) => { |
7
|
|
|
const value = element.value; |
8
|
|
|
if (element.getAttribute('type') === 'select') { |
9
|
|
|
return element[element.selectedIndex].value; |
10
|
|
|
} |
11
|
|
|
if (element.getAttribute('type') === 'radio') { |
12
|
|
|
const name = `[name=${fieldName}]:checked`; |
13
|
|
|
const checkedElement = document.body.querySelector(name); |
14
|
|
|
if (checkedElement !== null) { |
15
|
|
|
return checkedElement.value; |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
if (element.getAttribute('type') === 'checkbox') { |
19
|
|
|
const name = `[name="${fieldName}"]:checked`; |
20
|
|
|
const checkedElements = Array.from(document.body.querySelectorAll(name)); |
21
|
|
|
const valueArray = []; |
22
|
|
|
if (checkedElements.length > 0) { |
23
|
|
|
checkedElements.forEach((element) => { |
24
|
|
|
valueArray.push(element.value); |
25
|
|
|
}); |
26
|
|
|
return valueArray; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
return value; |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
const submitPartial = () => { |
33
|
|
|
const data = new FormData(); |
34
|
|
|
formElements().forEach((element) => { |
35
|
|
|
const fieldName = element.getAttribute('name'); |
36
|
|
|
const value = getElementValue(element, fieldName); |
37
|
|
|
if (!data.has(fieldName)) { |
38
|
|
|
if (typeof value === 'object') { |
39
|
|
|
value.forEach((arrayValue) => { |
40
|
|
|
data.append(fieldName, arrayValue); |
41
|
|
|
}) |
42
|
|
|
} else { |
43
|
|
|
if (value) { |
44
|
|
|
data.append(fieldName, value); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
}); |
49
|
|
|
const httpRequest = new XMLHttpRequest(); |
|
|
|
|
50
|
|
|
httpRequest.open('POST', `${baseDomain}${submitURL}`, true); |
51
|
|
|
httpRequest.send(data); |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
const attachSubmitPartial = (button) => { |
55
|
|
|
button.addEventListener('click', submitPartial); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
export default function () { |
59
|
|
|
buttons().forEach(attachSubmitPartial); |
60
|
|
|
} |
61
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.