question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Programmatically 'unchecking' a checkbox does not hide conditional content

See original GitHub issue

I’m using js to uncheck some checkboxes - however conditionally revealed content doesn’t hide like it should.

Talking with @nickcolley it sounds like the conditional logic listens for clicks rather than the checked attribute.

I can update my js to use clicks instead, but it feels like something to consider - whether this code should be testing the state of the input rather than clicks.

I imagine automated tests / other js that does stuff with checkboxes might also have this issue.


FWIW I’m adding a ‘none of these’ type option in my checkboxes that then deselects others. This is my code (written by @alex-ju a few years ago):

  // Deselect main checkboxes when none ticked
  // Deselect none when main checkboxes ticked
  var $selectButtons = $('*[data-js-select="select"]')
  var $deSelectButtons = $('*[data-js-select="deselect"]')

  $selectButtons.on('click', function () {
    $deSelectButtons.attr('checked', false)
  })
  $deSelectButtons.on('click', function () {
    $selectButtons.attr('checked', false)
  })

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
NickColleycommented, Aug 16, 2019

@Jordan-Hall thanks for the pointer.

It looks interesting but I don’t think it is appropriate here:

  • the proposal cannot observe DOM attribute changes, only DOM events
  • the proposal is only at stage 1 so it is not a native standard, it was proposed 2017, there is no indication that this has interest and will end up in ECMAScript

Whereas the other options are native standards that have wide browser support.

0reactions
NickColleycommented, Dec 12, 2019

I have done some more investigation into this and determined that Mutation Observers cannot work for this since they can only observe attribute changes not property changes.

This is the difference between element.value = 'test' and element.setAttribute('value', 'test').

Mutation Observers (ruled out)

I’ve created to show that only attribute changes can be observed: https://jsbin.com/qajudohulo/edit?js,console,output

Polling the DOM in a loop (broad support)

I imagine the issue with this is a performance impact since DOM queries can sometimes be expensive within a loop. https://jsbin.com/tuvuhek/3/edit?html,js,console,output

Patching Object.defineProperty (IE9+)

Patching the dom element with Object.defineProperty :

https://jsbin.com/ligezey/edit?html,js,console,output

ES6 Proxies (Edge, modern browsers)

ES6 Proxies work somewhat but our constructors for elements would need to return a proxied version of the element. https://jsbin.com/yubisoc/edit?html,js,console,output

Custom elements (Chromium browsers, Firefox, Safari (no support for [is]?))

Note - Polyfills exist for this approach to broaden support: https://github.com/ungap/custom-elements-builtin#all-possible-features-detections

Custom elements allow property to be observed:

https://jsbin.com/yeduzof/edit?html,js,console,output

Exposing methods from the component itself

Perhaps we could allow uses to set values via a custom method

const instance = new Component()
instance.init()
instance.setValue('test')
Read more comments on GitHub >

github_iconTop Results From Across the Web

uncheck checkbox programmatically in reactjs - Stack Overflow
1- You can use ref with check boxes, and onClick of button, by using ref you can unCheck the box. 2- You can...
Read more >
Boolean field: hide (in display) if unchecked - Drupal Answers
If this field is checked, only show the label (when viewing the page); If this field is unchecked, don't show the field at...
Read more >
How to Check or Uncheck A Checkbox Dynamically Using ...
Answer: Use the jQuery prop() method. You can use the jQuery prop() method to check or uncheck a checkbox dynamically such as on...
Read more >
How to Check/Uncheck the checkbox using JavaScript
Approach 1: Using Reset Button. Create a javascript function. Use window.addEventListener: It allows adding event listeners on any HTML ...
Read more >
Disable checkboxes for a few rows from the selectable ...
Hi, Sam, In order to deselect a row programmatically when the master checkbox is selected, try attaching the click event to it, for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found