37

How do you add boolean attributes using JavaScript? For example, how can you change:

<p> to <p contenteditable>

<p> to <p data-example>

5
  • Those are HTML attributes. Whether they are boolean in nature doesn't really mater. For contenteditable, you can treat it as a standard HTML attribute/value pair contenteditable="true" Commented Feb 8, 2012 at 21:13
  • @DA It does matter if you want to set them using the IDL property rather than through setAttribute(). For example, the selected boolean property, can receive the boolean values true or false, while the attribute value should either be selected (selected="selected") or the empty string (selected or selected=""). Commented Feb 8, 2012 at 21:21
  • 1
    @DA In the spec they're referred to as boolean attributes w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2 Commented Feb 8, 2012 at 21:23
  • 1
    contenteditable is not a boolean attribute. See my answer: stackoverflow.com/a/9201499/96656 Commented Feb 8, 2012 at 21:35
  • Thanks for the clarifications! Yes, I was incorrect. A true boolean variable can only have a value of itself. I guess what I was getting at is that regardless of whether or not it's boolean, you can treat it like any other html entity in that it can have a value. That said, I'm not up on what IDL is so that could very well be something entirely different. Commented Feb 8, 2012 at 22:56

6 Answers 6

49

To add a Boolean attribute:

node.setAttribute(attributeName, '');
// example:
document.body.setAttribute('hidden', '');

Note the empty string as the second argument!

Use node.removeAttribute(attributeName) to remove an attribute as mentioned by others.

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

2 Comments

This is the only answer that actually directly responds to the question of how to set a non-reflected boolean attribute.
Correct answer.
26

In general, you can use element.setAttribute('attributeName', 'value') or element.propertyName = value to toggle an element’s attributes or properties.

Boolean attributes

For boolean attributes, set the attribute with the same-named value:

element.setAttribute('disabled', 'disabled');

Removing a boolean attribute works the same way as other attributes:

element.removeAttribute('disabled');

However, neither of your two examples are boolean attributes!

contenteditable

contenteditable is not a boolean attribute, it’s an enumerated attribute. Its possible values are the empty string, "true", and "false".

While setAttribute seems overkill in this case, you could use it:

element.setAttribute('contenteditable', 'true');
// to undo:
element.removeAttribute('contenteditable');

The property name for the contenteditable attribute is contentEditable (note the capital E), and it recognizes the values 'true', 'false', and 'inherit' — so you could just use:

element.contentEditable = 'true';
// to undo:
element.contentEditable = 'false';

Note that 'true' and 'false' are strings here, not booleans.

data-example

For the data-example attribute, you could use:

element.setAttribute('data-example', 'some value'); // the value should be a string
// to undo:
element.removeAttribute('data-example');

Or, in browsers who support dataset (see the ones highlighted in light green on http://caniuse.com/dataset), you could use:

element.dataset.example = 'some value';

10 Comments

Ah yea enumerated :] Comparing an empty string "" === elem.getAttribute('contenteditable') is true in JavaScript. But if you compare it to any of those other values it's false. In the browser for contenteditable both "the empty string and the true keyword map to the true state" w3.org/TR/html5/editing.html#contenteditable
@ryanve Well of course; '' == '' in JavaScript, so if you use <p contenteditable> or <p contenteditable="">, p.getAttribute('contenteditable') == ''.
This does not answer the question.
@kleinfreund That’s incorrect. Custom attributes cannot be boolean attributes, by definition! You could kind of treat a custom attribute as if it was boolean, but it would still behave differently than a real boolean attribute.
@kleinfreund An example boolean attribute is hidden. <p hidden> or <p hidden=""> or <p hidden="hidden"> are all equivalent. Any attribute that is not defined as a boolean attribute in the spec (including custom attributes) does not magically get that behavior. For custom attributes you’d have to implement something like it yourself, and still it wouldn’t be a real boolean property as it wouldn’t go through the same pipeline in the browser.
|
4

To set an attribute

Use element.setAttribute: https://developer.mozilla.org/en/DOM/element.setAttribute

If you add an id like this:

<p id="p1">

you can select the element like this:

var p1 = document.getElementById("p1"); 

To set a Boolean attribute

According to the W3C HTML4 specification:

Boolean attributes may legally take a single value: the name of the attribute itself

so you can add your attribute like this:

p1.setAttribute("contenteditable", "contenteditable");

To set contentEditable

According to the W3C HTML5 specification, the attribute contentEditable can have values true, false and inherit. Then you would have to do something like this:

p1.setAttribute("contenteditable", "true");

To be honest, I am also not sure which one is best in your situation.

3 Comments

That makes it <p id="p1" contenteditable="true"> which is not the same as <p id="p1" contenteditable>
@ryanve It is the same. contenteditable is not a boolean attribute. See my answer: stackoverflow.com/a/9201499/96656 @TheNail You may want to revert that edit, as this has nothing to do with boolean attributes.
Is there a merge function for answers? :D
2
element.setAttribute('contenteditable','contenteditable')

or to clear:

element.removeAttribute('contenteditable')

Comments

1

Use element.dataset.example to modify the value of the data-example attribute.

1 Comment

I think to be equivalent you'd set it to an empty string. element.dataset.example = "" See stackoverflow.com/questions/9200712/…
-1

Quoted from MDN:

Boolean attributes are considered to be true if they're present on the element at all, regardless of their actual value; as a rule, you should specify the empty string ("") in value (some people use the attribute's name; this works but is non-standard).

source: https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

Comments

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.