Javascript: Submit JSON object from form values

PHOTO EMBED

Mon Aug 29 2022 21:05:14 GMT+0000 (Coordinated Universal Time)

Saved by @marcopinero #javascript

function getObjectFromForm(formSelector) {
    document.querySelector(formSelector)
    .addEventListener("submit", e => {
        e.preventDefault();
 
        const data = Object.fromEntries(new FormData(e.target));
 
        // then do what you want with data or JSON.stringify(data)
    });
}
content_copyCOPY

Vanilla js to get json objet from form values