Bootstrap Tutorial - Expanding and collapsing individual element via JavaScript








The following code shows how to expanding and collapsing individual element via JavaScript.

Example

<!--   w  w w . ja va2  s .  c om-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Creating Twitter Bootstrap 3 Collapsible Element via
  JavaScript</title>
<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(){
    $(".btn").click(function(){
        $("#toggleDemo").collapse('toggle');
    });
});
</script>
<style type="text/css">
p {
  background: #F9F9F9;
  border: 1px solid #E1E1E8;
  margin: 10px 0;
  padding: 8px;
}

.bs-example {
  margin: 20px;
}
</style>
</head>
<body>
  <div class="bs-example">
    <!-- Trigger Button HTML -->
    <input type="button" class="btn btn-primary" value="Toggle Button">
    <!-- Collapsible Element HTML -->
    <div id="toggleDemo">
      <p>
        This is a simple example of expanding and collapsing individual
        element via JavaScript. Click on the <b>Toggle Button</b> button to
        see the effect.
      </p>
    </div>
  </div>
</body>
</html>

Click to view the demo