5

Some HTML attributes are described in most docs as not having a value. Examples include:

<script async ...>
<option selected ...>
<input type="checkbox" checked ...>

Is there any functional/semantic difference between those and empty attributes?

<script async="" ...>
<option selected="" ...>
<input type="checkbox" checked="" ...>

I'm asking because I'm using some library to parse the HTML, then manipulating a few nodes, then serializing the nodes back into HTML and the serializer turns the former into the latter.

Is it fine with empty attribute values or should I work on getting the HTML back into the top format above?

2 Answers 2

8

Quote from w3c:

A boolean attribute without a value assigned to it (e.g. checked) is implicitly equivalent to one that has the empty string assigned to it (i.e. checked=""). As a consequence, it represents the true value.

Sign up to request clarification or add additional context in comments.

Comments

0

Functionally this should have the same effect, but empty attributes may cause XHTML validation issues, though depending on requirements/spec this probably won't be an issue. You can always check it on the W3C validators: https://validator.w3.org/

1 Comment

an attribute without a value will cause XHTML being invalid. Which brings up another point: If you know your markup will only be on your website and not to be used elsewhere, you can be relaxed on setting values to html element attributes. But if you know that your text/markup will be used elsewhere, I would highly recommend giving value to the attributes as it will cause problems for those who use your markup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.