-1

This script doesn't work on mobile chrome:

function check() {
    document.getElementById("sh").checked = false;
}

Can you recommend an alternative which work on Android Chrome?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Gergototh
  • 1
  • 1
  • Possible duplicate of [Check/Uncheck checkbox with JavaScript?](https://stackoverflow.com/questions/8206565/check-uncheck-checkbox-with-javascript) – Kos Jan 20 '18 at 21:30
  • What exactly doesn't work? When is the `check` function called and what result are you expecting? This is a very basic feature and I doubt it's not supported on chrome mobile. You should provide the HTML for your `sh` checkbox as well. – Khauri Jan 20 '18 at 21:58
  • I’m building a ‘css hack’ menu. I was testing six scripts. The scripts are good, they work perfectly on desktop and windows phone. On android the scripts don’t work. I don’t know why. That’s why I was starting build a java script menu on this morning, and i’ll leave this problem. Thanks for responds! – Gergototh Jan 21 '18 at 14:50

1 Answers1

1

I can give you alternatives that should work fine in modern desktop browsers but I have no idea about Android, so here goes...

Toggle its state...

sh.checked=!sh.checked; 

Remove the atttribute...

sh.removeAttribute('checked');

Btw, you don't need to use getElementById('') if you know the ID, just use the ID directly.

Btw, I'm not surprised that...

sh.checked = false;

...doesn't work because the checked attribute was not really designed to be a boolean.

Had it been a boolean then assigning "checked" to it would always equate to false.

Booleans can only accept true or false.

So the Android JS engine you're using has the correct behavior IMHO..

  • 1
    I've given two examples above where I am operating DIRECTLY on a known ID. I only use getElementById() without the quotes for variables. – user4723924 Jan 20 '18 at 21:55
  • 1
    Nope, an ID exists either because it was declared on an element in HTML or created dynamically in JS. There's no need to create variables. – user4723924 Jan 20 '18 at 22:00
  • 1
    @connexo https://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables – Khauri Jan 20 '18 at 22:04
  • Ok, mea culpa. Learned something new, thank you! Just do *any* change to your answer will allow me to turn my downvote into an upvote. – connexo Jan 20 '18 at 22:06