Sohail xIN3N
Sohail xIN3N

Reputation: 3041

How to use toggle buttons instead of checkboxes/radio buttons?

How to use ON/OFF switches instead of Checkboxes and Radio buttons as shown in image to beautify website design.

Toggle Buttons

Upvotes: 0

Views: 2886

Answers (3)

shin
shin

Reputation: 3659

Hope this helps. Check it out!

$(document).ready(function() {
  // Switch toggle
  $('.Switch').click(function() {
     $(this).toggleClass('On').toggleClass('Off');
  });
});

Upvotes: 3

Luca Davanzo
Luca Davanzo

Reputation: 21528

If you want to do with your hand, basically you have structure like this:

HTML

<div class='button' id='button-0'>
    <div class='button-switcher left' id='button-switcher-0'></div>
</div>

And with a script, you animate the inner div in this way:

JQuery

var switchButton = function() {
   $(this).toggleClass('left, right');
   var versus = $(this).hasClass('right') ? 1 : -1; 
   var targetLeft = versus * parseInt($(this).parent().css('width')) / 2;
   $(this).animate({
       'left': '+=' + targetLeft + "px"
   });
}

$('.button-switcher').on('click', switchButton);

Take a look to the working demo.

Upvotes: 1

Harshit Jain
Harshit Jain

Reputation: 492

The buttons UI can be designed using css and you can use the toggle method of jquery.

http://api.jqueryui.com/toggle/

After you have written the css for the buttons you just have to add css class and remove css class for the button.

Upvotes: 1

Related Questions