I'm trying to mark atextinput box asrequiredin Javascript.
If the field was originally markedrequired:
When the user attempts to submit, they receive a validation error:
But now I want to set therequiredproperty at "runtime" via Javascript:
Use the corresponding script:
//recommended W3C HTML5 syntax for boolean attributes document.getElementById("edName").attributes["required"] = "";
No validation checks, no blocking unless I submit now.
SettingsWhat is thecorrectway to validate Boolean attributesin HTML5?
jsFiddle
What is the value of this attribute you ask?
HTML5 ValidationrequiredAttributes logged asBoolean:
4.10.7.3.4requiredProperties
requiredThe property is a Boolean property. When specified, this element is required.
There are a lot of annoying things about how to definebooleanproperties. HTML5 specification comments:
The presence of a Boolean attribute on the
element represents a true value, and the absence of this attribute represents a false value.
If the attribute is present, its value must be the empty string or an ASCII case-insensitive value that matches the attribute's canonical name and has no leading or trailing spaces.
This means you can specify therequiredbooleanattribute in two different ways:
edName.attributes.required = ""; //the empty string edName.attributes.required = "required"; //the attribute's canonical name
But what is the value of the attributeactually?
When you look at my jsFiddle for this question, you'll notice that if therequiredattribute is defined in the tag:
Then the value of the attributeis not an empty string, nor is it the canonical name of the attribute:
edName.attributes.required = [object Attr]
This may lead to a solution.