Is there a difference in using HTML5 attributes with or without true / false?

7

I would like to know if there is a problem in the code when I do not assign a Boolean value to disabled , readonly or required . I'm working with Java in the backend and the IDE points to an error in html when I assign a Boolean value to the attribute, maybe because of xml , I do not know. Is there any kind of problem leaving without Boolean? Is there a rule for putting true or false on these Html attributes?

<input type="text" class="form-control" id="nmPessoa" required>      // aceito

<input type="text" class="form-control" id="nmPessoa" required="true"> //msg
  

MSG : Undefined attribute value (true).

    
asked by anonymous 10.07.2018 / 20:13

4 answers

8

These attributes are called Boolean attributes - and, unofficially, in some places, proprietary.

As predicted in the specification, only the presence of the attribute is already sufficient to consider as true, as well as its absence as false. In addition, to maintain backwards compatibility, the attribute has also been preserved for the attribute being able to receive a nonempty string with the same attribute name.

Therefore, required and required="required" are valid attributes.

In the specification itself says:

  

The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

That is, values like "true" and "false" are not allowed in boolean attributes (in others, yes). To set it to false, simply omit the attribute of the element.

So,

<input type="checkbox" checked name="cheese" disabled>

It is equivalent to

<input type="checkbox" checked="checked" name="cheese" disabled="disabled">

In addition, you can mix the patterns and omit the quotes:

<input type=checkbox checked=checked name=cheese disabled>

See working:

<input type="checkbox" checked name="cheese" disabled> Sem valores
<input type="checkbox" checked="checked" name="cheese" disabled="disabled"> Com valores
<input type=checkbox checked=checked name=cheese disabled> Misturado

Even though it's forecasted, the specification turns out to be just a suggestion of standardization, which does not mean that all browsers will implement it faithfully. I believe all major current browsers will see "true" as true, but this can change without notice in any version, in any browser. There's no reason to use it.

    
10.07.2018 / 20:33
9

You can use online tools to check this out. W3C itself has an online HTML and CSS validator

Note that when you use the attribute and put the Boolean value True or False the tool gives the alert.

HereistheofficialW3CdocumentationontheattributesofinputinHTML5

  • link
  

2.4.2. Boolean attributes

     

A number of attributes are boolean attributes. The presence of a boolean attribute on an element   represents the true value, and the absence of the attribute represents   the false value.

     

If the attribute is present, its value must either be the empty string   or a value that is an ASCII case-insensitive match for the attribute's   canonical name, with no leading or trailing white space.

In short, these are attributes that do not require True or False , because when they are absent they are False by default , but if they are present in input they are considered True by default . In fact, the True and False arguments are ignored by the browser , since they are not accepted values for this type of attribute, so they present the error in the validation.

Source: link

    
10.07.2018 / 20:29
6

The first is correct, the second may work by coincidence in some browsers. A string is considered a valid value, therefore true, so it just happens to work.

<input type="text" class="form-control" id="nmPessoa" required="false">

Is this still required? Yes, it continues, no matter what is written on it, a string with a content means true but passes the wrong idea that it is not required.

At least the HTML5 (what you are using, according to the question) says that the attribute has no values. Browsers often make the best effort to interpret and end up accepting a value, no matter what it is, the value will be ignored. In 100% valid code you should not place anything.

    
10.07.2018 / 20:23
2

Problem does not exist, HTML will identify and apply property independently whether it is true or false .

In the attribute documentation there is already a mention of this type of attribute in XHTML , or even if it is XHTML it is necessary to use required="required" , this also for disabled and readonly

If it's about HTML 4.01 and HTML5 the attribute can be left alone ...

    
10.07.2018 / 20:27