Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap: How to see the state of a toggle button?

How do I see the state of a toggle button?

With a checkbox I can listen to "changed" event of the checkbox and do a $(this).is(":checked") to see what state it has.

<a id="myId" class="btn" data-toggle="button"/> 

But not sure how to do that with a toggle button?

like image 340
Houman Avatar asked Nov 10 '12 16:11

Houman


People also ask

How do I toggle a button in bootstrap?

Add data-toggle="buttons" to a . btn-group containing those modified buttons to enable their toggling behavior via JavaScript and add . btn-group-toggle to style the <input> s within your buttons. Note that you can create single input-powered buttons or groups of them.

What is the difference between toggle button and button?

Toggle button means if you need to button with two option like Play/Stop in a button at that time on first click it will be working as a play button and second time working as a stop button this is toggle button.

What does toggle mean in bootstrap?

The data-toggle is an HTML-5 data attribute defined in Bootstrap. The advantage of using this is that, you can select a class or an id and hook up the element with a particular widget.


2 Answers

you can see what classes the button has..

$(this).hasClass('disabled') // for disabled states $(this).hasClass('active') // for active states $(this).is(':disabled') // for disabled buttons only 

the is(':disabled') works for buttons, but not the link btns

like image 141
Yohn Avatar answered Sep 27 '22 17:09

Yohn


If you're using jQuery to intercept the click event like so...

$(this).click(callback) 

you need to get creative because .hasClass('active') doesn't report the correct value. In the callback function put the following:

$(this).toggleClass('checked') $(this).hasClass('checked') 
like image 33
reubano Avatar answered Sep 27 '22 15:09

reubano