Is there a better way for checking an attribute for:
- it exist. so value must be false if attribute doesn’t exist
- Value is correct (boolean)
var isOwner = false;
if ($(selectedItem).is('[data-isOwner="True"]') || $(selectedItem).is('[data-isOwner="true"]')) {
isOwner = true;
} else {
isOwner = false;
}
Now I need to check for ‘True’ and ‘true’…
Thanks
You can convert the value stored in data-isOwner to lower case and only compare the value to ‘true’.
The above use of
<wanted-value> || ''will make it so that if the selectedItem doesn’t have the attribute data-isOwner the expression will result in an empty string, on which you can call toLowerCase without errors.Without this little hack you’d have to manually check so that the attribute is indeed present, otherwise you’d run into a runtime-error when trying to call toLowerCase on an undefined object.
If you find the previously mentioned solution confusing you could use something as