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

Asked

Viewed 418 times

8

There is a possibility of some kind of problem in the code when I do not assign a boolean value to the attributes disabled, readonly or required?

I’m working with Java in the back end and the IDE points to html error when I assign a boolean value to the attribute, maybe because of XML, I don’t know. There’s some kind of problem leaving without the Boolean? There is a rule to put true or false in 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).

  • 2

    Out of curiosity, where did you see this message?

  • Not really an error, it would be more like a warning message, gets exclamation signal inside a yellow triangle next to the line numbering.

  • But where is this exclamation mark? In the console, in the validator in W3.org?

  • Next to the IDE’s own line numbering.

4 answers

10

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

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

inserir a descrição da imagem aqui

Here is the official W3C documentation on the attributes of the input in HTML 5

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, for when she is absent they are False for default, but whether they are present in input are considered True for default. In fact the arguments True and False are ignored by browser, because they are not accepted values for this type of attribute, so they present the error in the validation.

Source: https://www.w3.org/TR/html5/infrastructure.html#sec-Boolean-Attributes

  • 1

    Legal this validator, I did not know!

  • 1

    @Leandrade note that he has several validation options, including you can copy and paste your entire document there, as long as it is only HTML, if you have js etc will present numerous errors. the W3C tb has a tool to validate the CSS worth looking at

  • Really, really cool top.

10


Such attributes are called boolean attributes - and, unofficially, in some places, owned.

As stated 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 retro-compatibility, the standard of the attribute being able to receive a string not empty with the same attribute name.

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

In the specification itself it says:

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

I mean, values like "true" and "false" are not allowed in boolean attributes (in others, yes). To set to false, just omit the element attribute.

Thus,

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

Is equivalent to

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

You can even 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 if foreseen, the specification turns out to be only one suggestion of standardization, which does not mean that all browsers will implement it faithfully. I believe all major current browsers will consider "true" as true, but this can change without notice in any version, in any browser. There is no reason to use.

6

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

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

I put in the Github for future reference.

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

At least the specification of HTML5 (what you are using, according to the question) says that the attribute has no values. Browsers usually make the best interpretation effort and end up accepting a value, even if no matter what it is, the value will be ignored. IN 100% valid code you should not put anything.

  • Maniero seems that true and false are not actually valid attributes, so the browser’s effort would simply be to decommission them as argument. So although it can work HTML will not be validated as you can test this validation link of W3C https://validator.w3.org/#validate_by_input+with_options

  • 1

    That’s what I wrote, I just used a wrong word, corrected now.

2

There is no problem, the HTML will identify and apply the property regardless if it is true or false.

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

When it comes to HTML 4.01 and HTML5 the attribute can be left alone...

  • A correction: it is not HTML that will identify this, but browsers. Since this behavior is not documented, it can be changed at any time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.