Check if an Element is Required using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 11, 2022

banner

Photo from Unsplash

Check if an Element is Required using JavaScript #

Use the required property to check if an element is required, e.g. if (element.required) {}. The required property returns true when the element is required, otherwise false is returned.

Here is the HTML for the examples in this article.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <input required type="text" id="first_name" name="first_name" /> <script src="index.js"></script> </body> </html>

And here is the related JavaScript code.

index.js
const element = document.getElementById('first_name'); console.log(element.required); // 👉️ true if (element.required) { console.log('✅ element is required'); } else { console.log('⛔️ element is not required'); } // ✅ Set attribute required // element.setAttribute('required', '') // ✅ Remove attribute required // element.removeAttribute('required');

The required property on an element returns true if the element is required and false otherwise.

When the required attribute is set on an element, the user must specify a value for the input before the form can be submitted.

You should always use the required property to check if an element is required.

Some examples online use the getAttribute() method on the element, however boolean attributes like required might not have a value.

index.js
const element = document.getElementById('first_name'); // 👇️ returns empty string console.log(element.getAttribute('required')); // ❌ DON'T DO THIS if (element.getAttribute('required')) { console.log('✅ element is required'); } else { console.log('⛔️ element is not required'); }

The getAttribute method returns the value of the provided attribute on the element.

However, it's a best practice to set boolean attributes, such as required, without a value.

If a boolean attribute is present at all, regardless of the value, its value is considered to be true.

If a boolean attribute is not present, the value of the attribute is considered to be false.

Therefore the required attribute could be set without a value on the element and the getAttribute method would return a false negative.

If you need to remove the required attribute from an element, use the removeAttribute() method.

index.js
const element = document.getElementById('first_name'); element.removeAttribute('required');

The removeAttribute method removes the provided attribute from the element.

If the attribute does not exist on the element, the method returns without throwing an error.

If you need to set the required attribute on an element, use the setAttribute() method.

index.js
const element = document.getElementById('first_name'); element.setAttribute('required', '');

The method takes the following 2 parameters:

  1. name - the name of the attribute to be set.
  2. value- the value to assign to the attribute.
We provided an empty string as the value for the required attribute because it's a best practice to set boolean attributes without a value.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.