HTML5 Boolean attributes always set
See original GitHub issue… yes, we shouldn’t complain now that there are massively efficient custom attributes!
Alas boolean attributes don’t work because they are set once the attribute exists, which means using valueNames
like
{
name: 'sticky',
attr: 'checked'
},
produces
<input class="sticky" checked="" type="checkbox">
which even with an empty value still counts as a set attribute (eg. checkbox is rendered checked)
Is there
- some way to not output empty attributes or
- override/filter the attribute output function or
- some other way I didn’t get?
As stated in the title this concerns all boolean attribures like disabled
, slected
, checked
and so on, which would be especially helpful for inline form elements.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:5
Top Results From Across the Web
Boolean attributes - HTML Spec
A number of attributes are boolean attributes . The presence of a boolean attribute on an element represents the true value, and the...
Read more >Boolean attributes - HTML 5
A number of attributes in HTML5 are boolean attributes . The presence of a boolean attribute on an element represents the true value,...
Read more >HTML attribute reference - HTML: HyperText Markup Language
Attribute Name Elements Description
accept‑charset List of supported charsets.
align, , , , , , , , , , , , , Specifies the horizontal...
Read more >Boolean HTML Attributes - javascript - Stack Overflow
Here's a list of boolean attributes in HTML5: github.com/kangax/html-minifier/issues/63 Note that it includes a few non-boolean attributes that ...
Read more >How to use boolean attributes in Web Components
There is no explicit way to set a boolean attribute to false, it must be removed. getAttribute returns null for non existent attributes,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t think CSS selectors containing
[attribute]
would be that difficult to get around. There are many like:That are used to select boolean attributes purely on whether or not a property exists.
The setAttribute logic should look something like this:
Am I missing something?
@mcglonelevi : This looks good! Thinking this over, a less embracing approach (e.g. w/o new functions or overrides) could be patched in the
setValue()
function like this – canceling the function if it tries to set an empty boolean attribute:–
What I was implying is that there might already be JS or CSS selector code that takes into account generated empty attributes (like
data-
attributes or emptysrc
values) and could potentially break current sites if we generally removed all empty attributes (e.g. no blacklisting).