{"id":372979,"date":"2022-09-08T06:12:51","date_gmt":"2022-09-08T13:12:51","guid":{"rendered":"https:\/\/css-tricks.com\/?p=372979"},"modified":"2022-09-08T06:12:52","modified_gmt":"2022-09-08T13:12:52","slug":"whats-new-with-forms-in-2022","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/whats-new-with-forms-in-2022\/","title":{"rendered":"What\u2019s New With Forms in 2022?"},"content":{"rendered":"\n
Browsers are constantly adding new HTML, JavaScript and CSS features. Here are some useful additions to working with forms that you might have missed\u2026<\/p>\n\n\n\n\n\n\n
requestSubmit()<\/code><\/h3>\n\n\nSafari 16<\/a> will be the final browser to add support for requestSubmit<\/code>.<\/p>\n\n\n\nBefore we look at how .requestSubmit()<\/code> works, let\u2019s remind ourselves how programmatically submitting a form with JavaScript works when using the .submit()<\/code> method. Submitting a form with submit()<\/code> does not trigger a submit event. So in the following code, the form is submitted, preventDefault()<\/code> has no effect, and nothing is logged to the console:<\/p>\n\n\n\nconst form = document.forms[0];\nform.addEventListener('submit', function(event) {\n \/\/ code to submit the form goes here\n event.preventDefault();\n console.log('form submitted!');\n});\n\ndocument.querySelector('.btn').addEventListener('click', function() {\n form.submit();\n})<\/code><\/pre>\n\n\n\n.submit()<\/code> will also ignore any HTML form validation. Given the following markup, the form will be submitted when the input is empty even though the input has a required<\/code> attribute:<\/p>\n\n\n\n<form> \n <label for=\"name\">Name<\/label>\n <input required name=\"name\" id=\"name\" type=\"text\">\n<\/form><\/code><\/pre>\n\n\n\n.requestSubmit()<\/code> is an alternative way to submit a form using JavaScript, but in contrast to .submit()<\/code>, HTML form validation will prevent the form from being submitted. If all the data entered in the form passes validation, the submit<\/code> event will be fired, meaning \u201cform submitted!\u201d would be logged to the console in the following example:<\/p>\n\n\n\nform.addEventListener('submit', function(event) {\n event.preventDefault();\n console.log('form submitted!');\n});\n\ndocument.querySelector('.btn').addEventListener('click', function() {\n form.requestSubmit();\n})<\/code><\/pre>\n\n\n\nYou could already achieve this by programmatically clicking the form\u2019s submit button, but requestSubmit<\/code> is perhaps a more elegant solution.<\/p>\n\n\nsubmitter<\/code> property of submit event<\/h3>\n\n\nThe SubmitEvent.submitter<\/code> property gained full cross-browser support<\/a> with the release of Safari 15.4. This read-only property specifies the <button><\/code> or <input type=\"submit\"><\/code> element that caused a form to be submitted.<\/p>\n\n\n\n<form>\n <button name=\"foo\" value=\"bar\" type=\"submit\">Bar<\/button>\n <button name=\"foo\" value=\"baz\" type=\"submit\">Baz<\/button>\n<\/form><\/code><\/pre>\n\n\n\nWhen you have multiple submit buttons or inputs, each with a different value, only the value of the button or input that was clicked on to submit the form will be sent to the server, rather than both values. That\u2019s nothing new. What is new is that the event listener for the submit event now has access to the event.submitter<\/code> property. You can use this to add a class to the button or input that triggered the form submission, for example, or to obtain its value<\/code> or any other of its HTML attributes.<\/p>\n\n\n\ndocument.forms[0].addEventListener('submit', function(event) {\n event.preventDefault();\n console.log(event.submitter.value);\n console.log(event.submitter.formaction);\n event.submitter.classList.add('spinner-animation');\n})<\/code><\/pre>\n\n\nformdata<\/code> event<\/h3>\n\n\n