Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  119] [ 6]  / answers: 1 / hits: 8069  / 2 Years ago, mon, may 23, 2022, 12:00:00

I have an HTML form with multiple inputs and a submit button. I created an eventlistener on the form that listens for 'submit' events. This passes a SubmitEvent into the callback function.


What I'm wondering, is there a getter or quick object property that can take in the input name (AKA input[name="XYZ"] and return what the input's value was at the time of submission?


More From » html

 Answers
8

No.


The target property of the event will reference the form element. From that you can access the form controls (via querySelector, elements or any other DOM method).


That will give you the values at the current time, not at the time the event fired.


If you want that, you'll need to copy the values before they change.




document.querySelector('form').addEventListener('submit', e => {
e.preventDefault();
const data = new FormData(e.target);
console.log([...data.entries()]);
});

<form>
<input name=one value=1>
<input name=two value=2>
<button>Submit</button>
</form>




[#126] Sunday, May 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
amari

Total Points: 736
Total Questions: 111
Total Answers: 90

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;