0

HTML :

<input type="checkbox" aria-checked="false" class="cloud-private" value="" data-spm-anchor-id="a800.io"> 

My java script : ( i want to click this checkbox if it is not checked ,but this does not work )

var z = document.getElementsByClassName('cloud-private'); if (z[0].aria-checked=='false'){ z[0].click(); } 
7
  • 1
    Does this answer your question? Get the value of checked checkbox? Commented Jun 9, 2022 at 19:15
  • this does not work: how does it fail? Does it do something else? How have you attempted to debug it? Is there an error in your console? Commented Jun 9, 2022 at 19:17
  • z[0].aria-checked doesn't do what you think. You can't have - in an identifier, that's being treated as the subtraction: z[0].aria - checked Commented Jun 9, 2022 at 19:23
  • 1
    You really shouldn't be using aria-checked at all, should be using the checked property. Any question like this can be easily resolved by simply taking the time to read the spec on MDN: developer.mozilla.org/en-US/docs/Web/HTML/Element/input/… Commented Jun 9, 2022 at 19:33
  • That doesn't mention anything about aria @StephenMIrving. The document on aria-checked does. Commented Jun 9, 2022 at 19:42

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Tks man , your solution works nice !!!
0

You can use querySelector to find the element with that class, and that aria-checked attribute. Then click it. Just make sure you spell things correctly (aria, not area, for example).

const selector = '.cloud-private[aria-checked="false"]'; const z = document.querySelector(selector); // If the element exists, check it z && z.click();
<input type="checkbox" aria-checked="false" class="cloud-private">

6 Comments

You should check if the selector returns anything.
Hi @Andy ,it does not work , "Uncaught SyntaxError: Identifier 'selector' has already been declared"
Do you have another variable called selector in your code? Just change the name of mine or just add the string directly to the querySelector. I only broke it up that way because it looks neater. @LeNguyen
@Andy ,it still not work even change to abc1000 "Uncaught SyntaxError: Identifier 'abc1000' has already been declared"
Tks @Andy , Stephen M Irving 's answer works .
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.