Bootstrap Tutorial - Put form to dropdown under navbar








The following code shows how to put form to dropdown under navbar.

Example

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'
  src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<style type='text/css'>
@import url('http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css');
</style><!--from w  ww. j  a v a2s .  c o m-->
<script type='text/javascript'>
$(window).load(function(){
    $('.this-works').on('shown.bs.dropdown', function () {
      var inputField = $(this).find("input:text").first();
      var x = setTimeout(function(){inputField.focus()}, 100);
    });
    $('.this-doesnt').on('shown.bs.dropdown', function () {
      var inputField = $(this).find("input:text").first().focus();
    });
});
</script>
</head>
<body>
<body>
  <div class="container">
    <!-- Static navbar -->
    <div class="navbar navbar-default">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse"
          data-target=".navbar-collapse">
          <span class="icon-bar"></span> 
          <span class="icon-bar"></span> 
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="#">Project name</a>
      </div>
      <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li><a href="#">Link</a></li>
          <li class="dropdown this-works">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">Works 
              <b class="caret"></b></i></a>
            <div class="dropdown-menu dropdown-form">
              <form class="form-inline" role="form">
                <div class="input-group">
                  <input type="text" class="form-control" placeholder="Search"
                    id="InputSearch" class="form-control"> 
                    <span class="input-group-btn">
                    <button class="btn btn-default" type="button">GO</button>
                  </span>
                </div>
                <!-- /input-group -->
              </form>
            </div></li>
          <li class="dropdown this-doesnt"><a href="#"
            class="dropdown-toggle" data-toggle="dropdown">Broken <b
              class="caret"></b></i></a>
            <div class="dropdown-menu dropdown-form">
              <form class="form-inline" role="form">
                <div class="input-group">
                  <input type="text" class="form-control" placeholder="Search"
                    id="InputSearch" class="form-control"> <span
                    class="input-group-btn">
                    <button class="btn btn-default" type="button">GO</button>
                  </span>
                </div>
                <!-- /input-group -->
              </form>
            </div></li>
        </ul>
      </div>
      <!--/.nav-collapse -->
    </div>
  </div>
</body>
</body>
</html>

Click to view the demo