var attr = ~\'input,textarea\'.indexOf( target.tagName.toLowerCase() )
? \'value\'
: \'innerHTML\'
I saw it in an answer, and
For those considering using the tilde trick to create a truthy value from an indexOf
result, it is more explicit and has less magic to instead use the includes method on String.
'hello world'.includes('hello') //=> true
'hello world'.includes('kittens') //=> false
Note that this is a new standard method as of ES 2015 so it won't work on older browsers. In cases where that matters, consider using the String.prototype.includes polyfill.
This feature is also available for arrays using the same syntax:
['apples', 'oranges', 'cherries'].includes('apples') //=> true
['apples', 'oranges', 'cherries'].includes('unicorns') //=> false
Here is the Array.prototype.includes polyfill if you need older browser support.