Answer copied from https://stackoverflow.com/a/78416452/1150462 with minor adaptation
This is impossible in HTML.
Attributes must have a value
According to the HTML standard:
Attributes have a name and a value... Attribute values are a mixture of text and character references, except with the additional restriction that the text cannot contain an ambiguous ampersand.
Why do I see attributes with no value?
The reason you see an attribute without a value in HTML code is because the HTML standard states:
Empty attribute syntax
Just the attribute name.
The value is implicitly the empty string.
Therefore, <div data-boolean-attribute></div> is simply a shortened form of <div data-boolean-attribute=""></div>.
So what should I do?
In modern JS, you should simply call setAttribute("data-boolean-attribute", ""). Your browser element inspector is free to display the attribute as data-body (Chrome) or data-body="" (Firefox).
(Shortened and adapted from https://8hob.io/posts/set-attribute-without-value-in-js/)
setAttributeis the only way. and data attributes are not boolean attributes. Even though you have some actual boolean attributes it is not invalid or wrong to assign them the value true such asreadonly="true"data-attributeif no value has to be associated to it?<div data-boolean-attribute></div>, or<input readonly>will produce<div data-boolean-attribute=""></div>, or<input readonly="">once serialized from the DOM.