How do you add boolean attributes using JavaScript? For example, how can you change:
<p> to <p contenteditable>
<p> to <p data-example>
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.
In general, you can use element.setAttribute('attributeName', 'value') or element.propertyName = value to toggle an element’s attributes or properties.
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!
contenteditablecontenteditable 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-exampleFor 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';
"" === 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'' == '' in JavaScript, so if you use <p contenteditable> or <p contenteditable="">, p.getAttribute('contenteditable') == ''.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.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");
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");
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.
<p id="p1" contenteditable="true"> which is not the same as <p id="p1" contenteditable>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.Use element.dataset.example to modify the value of the data-example attribute.
element.dataset.example = "" See stackoverflow.com/questions/9200712/…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
contenteditable="true"setAttribute(). For example, theselectedboolean property, can receive the boolean valuestrueorfalse, while the attribute value should either beselected(selected="selected") or the empty string (selectedorselected="").contenteditableis not a boolean attribute. See my answer: stackoverflow.com/a/9201499/96656