Last updated: Mar 5, 2024
Reading timeยท4 min

Use the disabled property to check if an element is disabled, e.g.
if (element.disabled) {}.
The disabled property returns true when the element is disabled, otherwise
false is returned.
Here is the HTML for the examples.
<!doctype html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <input disabled type="text" id="first_name" name="first_name" /> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const element = document.getElementById('first_name'); console.log(element.disabled); // ๐๏ธ true if (element.disabled) { console.log('โ element is disabled'); } else { console.log('โ๏ธ element is not disabled'); } // โ Set attribute disabled // element.setAttribute('disabled', '') // โ Remove the attribute disabled // element.removeAttribute('disabled');

The disabled property on an element returns true if the element is disabled
and false otherwise.
When the disabled attribute is set on the element, the element is not mutable and cannot be focused.
disabled property to check if an element is disabled.getAttribute() methodSome examples online use the getAttribute() method on the element. However,
boolean attributes like disabled might not have a value.
const element = document.getElementById('first_name'); // ๐๏ธ returns empty string console.log(element.getAttribute('disabled')); // โ DON'T DO THIS if (element.getAttribute('disabled')) { console.log('โ element is disabled'); } else { console.log('โ๏ธ element is not disabled'); }

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 disabled,
without a value.
true.If a boolean attribute is not present, the value of the attribute is considered
to be false.
Therefore, the disabled 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 disabled attribute from an
element, use the removeAttribute() method.
const element = document.getElementById('first_name'); // โ Remove attribute disabled element.removeAttribute('disabled');
The removeAttribute method removes the provided attribute from the element.
If you need to set the disabled attribute on an element, use the
setAttribute() method.
const element = document.getElementById('first_name'); // โ Set attribute disabled element.setAttribute('disabled', '');
The method takes the following 2 parameters:
name - the name of the attribute to be set.value- the value to assign to the attribute.We provided an empty string as the value for the disabled attribute because
it's a best practice to set boolean attributes without a value.
Use the required property to check if an element is required.
The required property returns true when the element is required, otherwise
false is returned.
Here is the HTML for the examples.
<!doctype html> <html lang="en"> <head> <title>bobbyhadz.com</title> <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.
const element = document.getElementById('first_name'); console.log(element.required); // ๐๏ธ true if (element.required) { console.log('โ The element is required'); } else { console.log('โ๏ธ The element is not required'); } // โ Set the required attribute // element.setAttribute('required', '') // โ Remove the required attribute // 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.
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.
const element = document.getElementById('first_name'); // ๐๏ธ returns an 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.
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.
const element = document.getElementById('first_name'); element.removeAttribute('required');
The removeAttribute() method removes the provided attribute from the element.
If you need to set the required attribute on an element, use the
setAttribute() method.
const element = document.getElementById('first_name'); element.setAttribute('required', '');
The method takes the following 2 parameters:
name - the name of the attribute to be set.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.