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

$().button('toggle') not working in 4.4.1 #30077

Closed
dkwebtec opened this issue Jan 24, 2020 · 17 comments
Closed

$().button('toggle') not working in 4.4.1 #30077

dkwebtec opened this issue Jan 24, 2020 · 17 comments
Labels

Comments

@dkwebtec
Copy link

dkwebtec commented Jan 24, 2020

$().button('toggle')

not working in 4.4.1, but working fine in 4.3.1

        <div class="btn-group-toggle" data-toggle="buttons">
            <label id="my-switch" class="btn btn-lg btn-light font-weight-bold ml-2 text-nowrap">
            <input type="checkbox" autocomplete="off" value="1"><span class="check">✓</span> <i class="far fa-clipboard"></i> <span class="d-none d-lg-inline">My</span>
            </label>
        </div>

$("#my-switch").button('toggle');

4.4.1 https://codepen.io/DK568/pen/MWYxWMO

4.3.1 https://codepen.io/DK568/pen/ExaMaxd

the difference is that button is not checked by jquery

@MartijnCuppens
Copy link
Member

Bug reports must include a live demo of the problem. Per our contributing guidelines, please create a reduced test case via CodePen/JS Bin or Stackblitz and report back with your link, Bootstrap version, and specific browser and OS details.

This is an automated reply

@dkwebtec
Copy link
Author

in my project when I revert from 4.4.1 to 4.3.1 without modifying anything else it starts working

@MartijnCuppens
Copy link
Member

Both demos seem to work fine?

@dkwebtec
Copy link
Author

What I see is that in 4.4.1 demo the toggle button is not active (checked)

the method is not working

@patrickhlauke
Copy link
Member

patrickhlauke commented Jan 24, 2020

forcing the checkbox input to be visible, i see that the checkbox does get correctly checked (just looking in the DOM tree in devtools or whatever doesn't actually show you the state of a checkbox)

image

(ah sorry, you mean checked as result of the method invocation in JS, i see what you mean now)

@patrickhlauke
Copy link
Member

confirming that button('toggle') seems broken for checkboxes (but not for radio buttons, oddly). tested by hacking away quickly at https://getbootstrap.com/docs/4.4/components/buttons/#button-plugin (giving the examples id attributes and trying to toggle them)

@XhmikosR
Copy link
Member

Does master work?

/CC @Johann-S

@XhmikosR
Copy link
Member

XhmikosR commented Mar 9, 2020

Ping @Johann-S ^^

@Lausselloic
Copy link
Contributor

Hi, I think the test need to be done on properties of the component and not on his attribute.
I've done the test on the same page as you @patrickhlauke and here's the result :

$('#check-test').is(':checked')
true
$('#check-test').prop('checked')
true
$('#check-test').attr('checked')
"checked"
$('#check-test').attr('checked')
"checked"
$('#check-test').prop('checked')
false
$('#check-test').is(':checked')
false

@Johann-S
Copy link
Member

@XhmikosR it should be the same in master

The things I can see, it's in v4.3.1 we trigger a change event and not in v4.4.1, maybe @patrickhlauke can help us to know where is the good behavior here

@patrickhlauke
Copy link
Member

i have lost track of how any of our JS work, so i'd go by actual behavior. make the actual checkbox/radio button visible in the CSS, run the button('toggle') and see if it has an actual effect...

@Johann-S
Copy link
Member

actually it does nothing 😟 for me it's related to this commit: 24abed1

but I don't know the correct behavior

@patrickhlauke
Copy link
Member

so i introduced the problem? gah, i absolutely detest the very existence of all that code. such a hacky construct we've been carrying around. admittedly, i haven't been able to trace the full logic of that code at the time either, it was a lot of very hacky trial and error to work around browser behaviors and bugs. can't wait for the CSS-only approach in v5...

i think fundamentally the problem we're having is that browsers do most of the work by default when the <label> is clicked or when the checkbox/radio are toggled with the keyboard. my hacky code tried to prevent problems when activating with the keyboard, which was previously preventDefaulted when it shouldn't have been. but having fixed THAT aspect, the programmatic toggling is now borked. i'll admit that right at this point, I'm not sure how to tackle this...

@Lausselloic
Copy link
Contributor

Hi, I wanted to help on that but can't clearly identify what is a bug, I've the same result on 4.3 and 4.4

There's no need to trigger the toggle on the label itself like in the pen, the button auto-init with the data-toggle
I've tried on latest chrome and Firefox on windows with the following html

<label class="btn btn-secondary">
    <input type="checkbox" autocomplete="off" value="1" id="myc">
    <span class="check">✓</span> <i class="far fa-clipboard"></i> <span class="d-none d-lg-inline">My</span>
  </label>

And running $('#myc')[0].checked into the console return the right value (true or false) when changing the state with keyboard or mouse

So I can't understand what's the error, if someone could explain to me

@patrickhlauke
Copy link
Member

the problem is: if you try to toggle programmatically with the method, it doesn't work (it's not about auto-initialising or anything...it's that at any point when you try and trigger the method, it does nothing, but it should work exactly the same way as clicking/activating with keyboard)

Lausselloic added a commit to Lausselloic/bootstrap that referenced this issue Mar 12, 2020
…nside label

add unit test to avoid further errors

Need to check on V5 without Jquery button plugin have the previous bug (checked state not change)
@XhmikosR
Copy link
Member

Fixed in #30388

@dkwebtec
Copy link
Author

I have tried this issue under version 4.5.0 and the method seems working but the change event is fired 3 times on each button click

https://codepen.io/DK568/pen/MWYxWMO

try to click the button

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants