/
modify-localized-form-input-with-javascript.html
155 lines (119 loc) · 5.68 KB
/
modify-localized-form-input-with-javascript.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<script>
// Changing a input value programmatically on Squarespace Localized Forms update in 2023
// Changing a input value programmatically on Squarespace Localized Forms update in 2023
// Changing a input value programmatically on Squarespace Localized Forms update in 2023
// Option 1 source: https://stackoverflow.com/questions/43438258/set-react-input-field-value-from-javascript-or-jquery/60378508#60378508
function changeValueVersion1(input, value) {
var nativeInputValueSetter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
"value"
).set;
nativeInputValueSetter.call(input, value);
var inputEvent = new Event("input", {
bubbles: true
});
input.dispatchEvent(inputEvent);
}
// Option 2
function changeValueVersion2(input, value) {
Object.defineProperty(input, "value", {
value: value, // Programmatically changed value
writable: true
});
var inputEvent = new Event('input', {
bubbles: true
});
input.dispatchEvent(inputEvent);
}
/*
In case you want to make immediate modifications on the form with the above methods.
We need to check when the Squarespace React form was added
*/
// 1. targetNode is the container or the form. If you have multiple forms, make sure to target the ".form-block" of that individual form.
// 2. Inside the function startMyChangesOnTheForm(), put the code that makes changes on the values of the form
document.addEventListener("DOMContentLoaded", function(){
var targetNode = document.querySelector('.form-block');
// Options for the observer (which mutations to observe)
var config = { childList: true, subtree: true };
// Callback function to execute when mutations are observed
var callback = (mutationList, observer) => {
mutationList.forEach(mutation => {
// debugger;
if( mutation.addedNodes.length ) {
mutation.addedNodes.forEach(addedNode => {
if(addedNode.classList.contains("react-form-contents")){
console.warn("Found react form mutation");
observer.disconnect();
startMyChangesOnTheForm();
}
});
}
});
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Here is where we can add our code that will make changes to the form
function startMyChangesOnTheForm() {
// Do the programmatical canges on the form
}
});
</script>
<!-- Working example of the two methods -->
<!-- Working example of the two methods -->
<!-- Working example of the two methods -->
<!-- Working example of the two methods -->
<script>
// Changing a input value programmatically on Squarespace Localized Forms update in 2023
// Changing a input value programmatically on Squarespace Localized Forms update in 2023
// Changing a input value programmatically on Squarespace Localized Forms update in 2023
// Option 2
function changeValueVersion2(input, value) {
Object.defineProperty(input, "value", {
value: value, // Programmatically changed value
writable: true
});
var inputEvent = new Event('input', {
bubbles: true
});
input.dispatchEvent(inputEvent);
}
/*
In case you want to make immediate modifications on the form with the above methods.
We need to check when the Squarespace React form was added
*/
// 1. targetNode is the container or the form. If you have multiple forms, make sure to target the ".form-block" of that individual form.
// 2. Inside the function startMyChangesOnTheForm(), put the code that makes changes on the values of the form
document.addEventListener("DOMContentLoaded", function(){
var targetNode = document.querySelector('.form-block');
// Options for the observer (which mutations to observe)
var config = { childList: true, subtree: true };
// Callback function to execute when mutations are observed
var callback = (mutationList, observer) => {
mutationList.forEach(mutation => {
// debugger;
if( mutation.addedNodes.length ) {
mutation.addedNodes.forEach(addedNode => {
if(addedNode.classList.contains("react-form-contents")){
console.warn("Found react form mutation");
observer.disconnect();
startMyChangesOnTheForm();
}
});
}
});
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Here is where we can add our code that will make changes to the form
function startMyChangesOnTheForm() {
// make sure the text element exists on your form
var myTextInput = document.querySelector(".form-wrapper .text input")
// Do the programmatical canges on the form
changeValueVersion2(myTextInput, "abZ Global")
}
});
</script>