Consider, we have a following
input element in our html.
<input placeholder="Name" id="name"/>
Now, we need to set the required attribute to the above element.
const input = document.getElementById("name");
Now, it has a
required property by using that we can set the required attribute to the element.
input.required = true;
Note: required attribute is a boolean attribute.
To remove it from the element, set
false to the
input.required = false;