Should not be using aria-checked
here in this way. Should be using the checked
attribute as is shown in the checkbox spec on MDN.
let cloudPrivateCbxs = document.querySelectorAll('.cloud-private');
if (cloudPrivateCbxs && !cloudPrivateCbxs[0].checked) {
cloudPrivateCbxs[0].click();
}
Here I used a useful variable names (never use single letter variable names except for simple counters like those in for
loops), used querySelectorAll
instead of getElementsByClassName
, then I first check if the variable contains anything, then I simply check the checked
attribute to see if it returns false. It is returning a boolean so I can use the logical NOT operator !
rather than check directly if it is equal to false.