Bootstrap Tutorial - Create toggle Button via Javascript








The following code shows how to create toggle Button via Javascript.

Example

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet"
  href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet"
  href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">
<script type="text/javascript"
  src="http://code.jquery.com/jquery.min.js"></script>
<script
  src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!--  ww  w.  j a  va 2 s.  c  om-->
    $("#myButtons .btn").click(function(){
      $(this).button('toggle');
    });
});
</script>
<style type="text/css">
.bs-example {
  margin: 20px;
}
</style>
</head>
<body>
  <div id="myButtons" class="bs-example">
    <button type="button" class="btn btn-default">Default</button>
    <button type="button" class="btn btn-primary">Primary</button>
    <button type="button" class="btn btn-success">Success</button>
    <button type="button" class="btn btn-info">Info</button>
    <button type="button" class="btn btn-warning">Warning</button>
    <button type="button" class="btn btn-danger">Danger</button>
  </div>
</body>
</html>

Click to view the demo