Public
Script
October 23, 2023
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
export function valForm(functionIWantIntoAForm) {
let inputFields = "";
for (let i = 0; i < functionIWantIntoAForm.length; i++) {
inputFields += `<input type="text" id="input${i}" name="input${i}"><br>`;
}
const formHTML = `
<form id="myForm">
${inputFields}
<input type="submit" value="Submit">
</form>
<p id="result"></p>
<script>
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault();
const formData = new FormData(event.target);
const args = Array.from(formData.values()).map(Number);
fetch('https://api.val.town/v1/run/rodrigotello.multiplicationFunctionTest', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ args }),
})
.then(response => response.json())
.then(data => {
// Log the entire data object to the console for inspection
console.log('Data:', data);
document.getElementById('result').textContent = 'Result: ' + data; // Use data directly
})
.catch(error => {
console.error('Error:', error);
});
});
</script>
`;
return formHTML;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023