I would like to enrich one of my forms fields automatically.
The server will send the client a date in ISO8601 format, and I would like to populate the DatePicker with that time.
Here is a code snippet.
$(document).on('knack-view-render.view_18', function(event, scene, records) {
$("input#field_20").on("change paste keyup", function() {
let link = $(this).val();
fetchData(link, function(err, data){
if(err) {
console.log(err);
return;
}
// #view_18-field_23 is a DatePicker
$("#view_18-field_23").val(data.iso8601Date);
});
});
function fetchData(someURL, callback) {
let someAPI = someURL;
fetch(someAPI, {
}).then(function(response) {
var contentType = response.headers.get("content-type");
if(contentType && contentType.includes("application/json")) {
return response.json();
}
throw new TypeError("Oops, we haven't got a JSON!");
})
.then(function(json) {
console.log(json.data);
callback(null, json.data);
})
.catch(function(error) {
console.log(error);
callback(error, null);
});
}