question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

  1. some way to not output empty attributes or
  2. override/filter the attribute output function or
  3. 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:open
  • Created 6 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
mcglonelevicommented, May 30, 2018

I don’t think CSS selectors containing [attribute] would be that difficult to get around. There are many like:

:disabled
:checked
:optional
:enabled

That are used to select boolean attributes purely on whether or not a property exists.

The setAttribute logic should look something like this:

const blacklist = ['disabled', 'checked'];

function setAttribute(name, value) {
    if (blacklist.indexOf(name) === -1) {
       // run regular setAttr logic
    } else {
        // run logic to add/remove attr based on true/false
    }
}

Am I missing something?

0reactions
Stolzenhaincommented, May 31, 2018

@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:

// make sure HTML5 booleean attributes don't contain empty values
const blacklist = ['disabled', 'checked'];
if (
  (value.length < 1) && //empty value
  (blacklist.indexOf(name) > -1) //
)
  return;

I don’t think CSS selectors containing [attribute] would be that difficult to get around. There are many like:

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 empty src values) and could potentially break current sites if we generally removed all empty attributes (e.g. no blacklisting).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found