1

I want to create an element like the following with JavaScript:

<div data-boolean-attribute></div>

Passing null or undefined to setAttribute() or the dataset property results in a value of the literal strings 'null' and 'undefined'. Passing an empty string or array to either method results in an empty string value:

<div data-boolean-attribute=""></div>

Is it possible to create a completely empty data attribute in vanilla JavaScript without using something like innerHTML?

5
  • setAttribute is 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 as readonly="true" Commented Jul 12, 2023 at 18:59
  • Is there a specific reason you need to do this? Commented Jul 12, 2023 at 19:01
  • 1
    Having an empty value or true/false etc are all valid and you can access them via javascript and CSS. So what is the specific reason why you don't want a value associated? Commented Jul 12, 2023 at 19:06
  • 1
    whats the point of setting a data-attribute if no value has to be associated to it? Commented Jul 12, 2023 at 19:08
  • Even the markup <div data-boolean-attribute></div>, or <input readonly> will produce <div data-boolean-attribute=""></div>, or <input readonly=""> once serialized from the DOM. Commented May 2, 2024 at 0:53

3 Answers 3

2

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/)

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

Comments

0

Create a new div element, then set data attr to empty string, and append it to the DOM body.

const div = document.createElement('div');
div.dataset.booleanAttribute = '';
document.body.appendChild(div);

Comments

0

In Chrome, using both dataset and setAttribute lets you pass an empty string and it won't add a value.

const test = document.querySelector("#test")
test.dataset.testa = "";
test.setAttribute("data-testB","");
console.log(test)
<div id="test">TEST</div>

2 Comments

@tacoshy, which is odd. I get data attributes without the empty value. My results are exactly what they are looking for.
@tacoshy This does appear to work as intended in Chrome. I used Firefox for testing as I didn't expect behavior to vary across browsers. I also use they/them pronouns.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.