- From: Jonathan Kingston <notifications@github.com>
- Date: Mon, 22 May 2017 10:05:19 -0700
- To: whatwg/dom <dom@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
Received on Monday, 22 May 2017 17:05:53 UTC
I often want to control boolean attributes with a property, forgetting that I have to write boiler plate to `setAttribute`/`removeAttribute` when other libraries provide convenience functions that are based upon the type or the attribute being set.
jQuery has two methods `attr()` and `prop()` which make managing boolean attributes much simpler:
```
$el.prop("hidden", true); // <el hidden="" />
$el.prop("hidden", false); // <el />
```
and:
```
$el.attr("hidden", true); // <el hidden="hidden" />
$el.attr("hidden", false); // <el />
```
Perhaps it doesn't make sense to implement this for all attributes, instead provide just:
```
el.setBoolAttribute("");
```
Which would function like:
```
Element.prototype.setBoolAttribute = function (attr, bool) {
if (bool) {
this.setAttribute(attr, "");
} else {
this.removeAttribute(attr);
}
};
```
That being said `attr` could implement this too, where boolean attributes to the second argument would behave different to a string:
```
el.attr("hidden", "false"); // <el hidden="false" />
el.attr("hidden", false); // <el />
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/461
Received on Monday, 22 May 2017 17:05:53 UTC