Categories
JavaScript Answers

How to Get the Form that Contains the Input Element with JavaScript?

Spread the love

We can get the form that contains the input element with the form property of the input.

For instance, if we have the following form:

<form>  
  <input>  
</form>

Then we can get the form that contains the input by writing:

const input = document.querySelector('input')  
console.log(input.form)

We just use the form property to get the form element that contains the input.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *