trusktr
2020-12-30
For example,
// Assume we start with `el` already having the `foo` attribute: el.toggleAttribute('foo', 'bar') // no longer has attribute `foo` el.toggleAttribute('foo', 'bar') // now has attribute `foo` with value `bar` el.toggleAttribute('foo', 'bar') // no longer has attribute `foo` el.toggleAttribute('foo', 'bar') // now has attribute `foo` with value `bar`
- If the attribute exists, remove it. This is similar to calling
removeAttribute('foo')while the attribute exists.- If the attribute does not exist, add it, with the given value. This is similar to calling
setAttribute('foo', 'bar')while the attribute does not exist.The above code would be equivalent to this:
// Assume we start with `el` already having the `foo` attribute: el.removeAttribute('foo') // no longer has attribute `foo` el.setAttribute('foo', 'bar') // now has attribute `foo` with value `bar` el.removeAttribute('foo') // no longer has attribute `foo` el.setAttribute('foo', 'bar') // now has attribute `foo` with value `bar`