Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle is not binded to checkbox value nor checked property #140

Closed
milmih opened this issue Nov 29, 2016 · 1 comment
Closed

Toggle is not binded to checkbox value nor checked property #140

milmih opened this issue Nov 29, 2016 · 1 comment

Comments

@milmih
Copy link

milmih commented Nov 29, 2016

Note

Possible duplicate of #111.

Problem

If you open http://www.bootstraptoggle.com/, fire up devtools console and run (checkbox is on, toggle is on):

>  $('.example input:first').prop('checked')
<- true
>  $('.example input:first').prop('checked', false)
<- [input]
>  $('.example input:first').prop('checked')
<- false

or:

>  $('.example input:first').val()
<- "on"
>  $('.example input:first').val('off')
<- [input]
>  $('.example input:first').val()
<- "off"

toggle doesn't get updated.

Also, toggle first changes it state upon click and then onChange event on checkbox input is invoked.

Proposed solution

  1. Bind the state of toggle to the state of checkbox.
  2. Do not assume that state will be changed (since it can be prevented in onChange event handler or in other ways). Update the toggle state upon checkbox state has been updated.
@stosh15x
Copy link

stosh15x commented May 24, 2018

This is my solution

$("input:checkbox").on('change', function(){
  var isChecked = $(this).is(':checked')
  $(this).val(Number(isChecked)).attr('checked', isChecked)
});

@milmih milmih closed this as completed Mar 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants