Skip to content

Getting input values from submit events #22

Description

@oliverjam

I know it's the weekend but I remembered I forgot to show you an easier way to grab all the inputs from a submit event.

You can use event.target.elements to get an HTMLFormControlsCollection, which contains all the inputs submitted in the form. This also conveniently has the group of radio buttons under a key matching the name you set on the radios. So in your case event.target.elements.description gets you a RadioNodeList of your four radio inputs. You can get the selected value of the radio group with event.elements.description.value.

So you can replace the whole form.map... thing with const answer = event.elements.description.value 😁


One thing I would note is that it's more common (and often a lot simpler) to use "controlled components" to keep all your form state in JavaScript (rather than having to awkwardly get it from the DOM). The final section of Monday's workshop has more info on it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions