Set Focus on Input Field or Textarea Inside a Modal on Activation - HTML CSS Bootstrap

HTML CSS examples for Bootstrap:Modal

Description

Set Focus on Input Field or Textarea Inside a Modal on Activation

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
 <head> 
  <title>Example of Setting Focus on First Text Input inside a Bootstrap Modal</title> 
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"> 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
  <script type="text/javascript">
$(document).ready(function(){
    $("#myModal").on('shown.bs.modal', function(){
        $(this).find('input[type="text"]').focus();
    });<!-- ww  w  .ja va  2  s .  c  o m-->
});
</script> 
 </head> 
 <body> 
  <div> 
   <a href="#myModal" class="btn btn-lg btn-primary" data-toggle="modal">Launch Demo Modal</a> 
   <div id="myModal" class="modal fade"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button> 
       <h4 class="modal-title">Your Feedback</h4> 
      </div> 
      <div class="modal-body"> 
       <form> 
        <div class="form-group"> 
         <label for="inputName">Name</label> 
         <input type="text" class="form-control" id="inputName"> 
        </div> 
        <div class="form-group"> 
         <label for="inputComment">Comments</label> &lt;textarea class="form-control" id="inputComment" rows="4"&gt;&lt;/textarea&gt; 
        </div> 
       </form> 
      </div> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
       <button type="button" class="btn btn-primary">Send</button> 
      </div> 
     </div> 
    </div> 
   </div> 
  </div>   
 </body>
</html>

Related Tutorials