I'm trying to mark a text
input box as required in Javascript.
<input id="edName" type="text" id="name">
If the field was originally marked required
:
<form> <input id="edName" type="text" id="name" required><br> <input type="submit" value="Search"> </form>
When the user attempts to submit, they receive a validation error:
But now I want to set the required
property at "runtime" via Javascript:
<form> <input id="edName" type="text" id="name"><br> <input type="submit" value="Search"> </form>
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.
Settings What is the correct way to validate Boolean attributes in HTML5?
jsFiddle
HTML5 Validation required
Attributes logged as Boolean
:
4.10.7.3.4
required
Properties
required
The property is a Boolean property. When specified, this element is required.
There are a lot of annoying things about how to define boolean
properties. HTML5 specification comments:
The presence of a Boolean attribute on theelement 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 the required
boolean attribute in two different ways:
edName.attributes.required = ""; //the empty string edName.attributes.required = "required"; //the attribute's canonical name
When you look at my jsFiddle for this question, you'll notice that if the required
attribute is defined in the tag:
<input id="edName" type="text" id="name" required>
Then the value of the attribute is not an empty string , nor is it the canonical name of the attribute:
edName.attributes.required = [object Attr]
This may lead to a solution.
required
is a reflective attribute (e.g.id
,name
,type
, etc.), so:...where
element
is the actualinput
DOM element, for example:(Just for completeness.)
reply:
That's because
required
in this code is a property object, not a string;property
isNamedNodeMap Its value isAttr代码>object
. To get the value of one of these, you need to look at itsvalue
property. But for boolean properties, the value is irrelevant; the property is either present in the map (true) or not (false).So if
required
is not reflected, you can set it by adding the attribute:...Equivalent to
element.required = true
. You can clear it by deleting it completely:...Equivalent to
element.required = false
.But we don't have to do this for
required
because it's already reflected.Short version
Long version
Once T.J. Crowder managed to point out the reflected properties, I learned the following syntax error:
You must pass
element.getAttribute
andelement.setAttribute
:This is because the attribute actually contains a special HtmlAttribute object:
By setting the attribute value to "true", you are mistakenly setting it to a String object instead of the HtmlAttribute object it requires:
Conceptually, the correct idea (expressed in type language) is:
Here’s why:
getAttribute(name)
setAttribute(name, value)
exist. They are responsible for assigning values to the internal HtmlAttribute objects.
In addition, it also reflects some attributes. This means you have better access to them via Javascript:
What you don't want to do is incorrectly use the
.attributes
collection:Test Case
This results in testing around the use of the
required
attribute, comparing the value returned via that attribute to the reflected attributeresult:
It is an error to attempt to access the
.attributes
collection directly. It returns an object representing a DOM property:This explains why you should never talk directly to the
.attributes
collection. You are not manipulating the value of the property, you are manipulating the object that represents the property itself.How to set required fields?
What is the correct way to set
required
on a property? You have two options, either reflected properties or by setting the properties correctly:Strictly speaking, any other value will "set" the property. However, the definition of the
Boolean
property states that it can only be set to the empty string""
to indicate true. The following methods can set therequired
Boolean attribute,But don’t use them:
We have learned that it is an error to try to set the property directly:
How to clear required fields?
TryRemove
required
The trick when using an attribute is that it's easy to accidentally open it:Invalid method:
When using reflection's
.required
property, you can also use any "falsey" value to turn it off and use a true value to turn it on. But for the sake of clarity, stick to right and wrong.How to check if is
required
?Check whether the attribute exists through the
.hasAttribute("required")
method:You can also check via the Boolean reflected
.required
property: