Home » Jquery » javascript – make the first tab the active tab when open a bootstrap modal-Exceptionshub

javascript – make the first tab the active tab when open a bootstrap modal-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

i have a button that will show a bootstrap modal when clicks on it
the modal contains sidebar tabs.

my problem is when i close the modal and open it again by clicking on the button it will open with the last activated tab
i want the activated tab to be the first tab once i close the modal and open the modal again without reloading the page.

my html

 // the button that will open the bootstrap modal  
            <div>
            <a href="#"  class="popupLink" data-toggle="modal" data-target="#yDetailsModal"> 
            </a>
            </div>

// bootstrap modal  


<div class="modal" id="MyDetailsModal" tabindex="-1" role="dialog" arialabelledby="myDetailsModal" aria-hidden="true" >

 <div class="modal-dialog modal-details" role="document">

  <div class="modal-content modal-details">

   <div class="modal-header">

 <button type="button" class="close close-button"  data-dismiss="modal" aria-label="Close" id="btnId"> </button>

  </div>


 <div class="modal-body">
  <div class=" tabs-left" id= "modal-tabs">
   <ul class="nav nav-tabs">

   <li class="active"> 
   <a href="#personal"  data-toggle="tab"></a></li>

  <li>
   <a  href="#promotion" data-toggle="tab"> </a> </li> 
    <li>
    <a  href="#vacation" data-toggle="tab"> </a> </li>


</ul>
 </div>



<div class="tab-content" id="tab-content">
 <div class="tab-pane active" id="personalInformation"> 
  <div>
   <ul>
     <li>

      <span> this is a personal information</span>
      </li>
</ul>
</div>
</div>






How to&Answer:

Can you add below mentioned jQuery Hope this help you.

$(".popupLink").click(function(){
    jQuery('.nav-tabs li').removeClass('active');
    jQuery('.nav-tabs li:first-child').addClass('active');
});

Answer:

Give it a try. (Run in a snippet to test)

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Bootstrap Modal Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div>
    <button type="button" onclick="openModal();" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
</div>
<div class="modal" id="myModal" tabindex="-1" role="dialog" arialabelledby="myModal" aria-hidden="true">
    <div class="modal-dialog modal-details" role="document">
        <div class="modal-content modal-details">
            <div class="modal-header">
                <button type="button" class="close close-button" data-dismiss="modal" aria-label="Close" id="btnId"></button>
            </div>
            <div class="modal-body">
                <div class=" tabs-left" id="modal-tabs">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#one" data-toggle="tab">One</a></li>
                        <li><a href="#two" data-toggle="tab">Two</a></li>
                        <li><a href="#three" data-toggle="tab">Three</a></li>
                    </ul>
                    <div class="tab-content">
                        <div id="one" class="tab-pane fade in active">
                            <h3>Menu 1</h3>
                            <p>Some content in menu 1.</p>
                        </div>
                        <div id="two" class="tab-pane fade">
                            <h3>Menu 2</h3>
                            <p>Some content in menu 2.</p>
                        </div>
                        <div id="three" class="tab-pane fade">
                            <h3>Menu 2</h3>
                            <p>Some content in menu 3.</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    function openModal() {
        $("#myModal li").first().children()[0].click();
    }
</script>
</html>

Answer:

Just use the Bootstrap default methods modal and tab

$('#MyDetailsModal').on('show.bs.modal', function (event) { 
    $(this).find('.nav a:first').tab('show');
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<div>
    <a href="#" class="popupLink btn btn-info btn-lg" data-toggle="modal" data-target="#MyDetailsModal">MyDetailsModal
    </a>
</div>

<div class="modal" id="MyDetailsModal" tabindex="-1" role="dialog" arialabelledby="myDetailsModal" aria-hidden="true">
    <div class="modal-dialog modal-details" role="document">
        <div class="modal-content modal-details">
            <div class="modal-header">
                <button type="button" class="close close-button" data-dismiss="modal" aria-label="Close" id="btnId"> </button>
            </div>

            <div class="modal-body">
                <div class=" tabs-left" id="modal-tabs">
                    <ul class="nav nav-tabs">
                        <li class="active">
                            <a href="#personal" data-toggle="tab">personal</a>
                        </li>
                        <li>
                            <a href="#promotion" data-toggle="tab">promotion</a>
                        </li>
                        <li>
                            <a href="#vacation" data-toggle="tab">vacation</a>
                        </li>
                    </ul>
                </div>

                <div class="tab-content" id="tab-content">
                    <div class="tab-pane active" id="personal">
                        this is a personal information
                    </div>
                    <div class="tab-pane" id="promotion">
                        this is a promotion information
                    </div>
                    <div class="tab-pane" id="vacation">
                        this is a vacation information
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Answer:

Calling a function ActiveFirstTab() before the model pop up

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

  <div>

    <button type="button" onclick="ActiveFirstTab();" class="btn btn-info btn-lg" data-toggle="modal" data-target="#MyDetailsModal">Open Modal</button>
  </div>



  <div class="modal" id="MyDetailsModal" tabindex="-1" role="dialog" arialabelledby="myDetailsModal" aria-hidden="true" >

   <div class="modal-dialog modal-details" role="document">

    <div class="modal-content modal-details">

     <div class="modal-header">

       <button type="button" class="close close-button"  data-dismiss="modal" aria-label="Close" id="btnId"> </button>

     </div>


     <div class="modal-body">
      <div class=" tabs-left" id= "modal-tabs">
       <ul class="nav nav-tabs">

         <li class="active"> 
          <a href="#personal"  data-toggle="tab">One</a></li>

          <li>
           <a  href="#promotion" data-toggle="tab">Two </a> </li> 
           <li>
            <a  href="#vacation" data-toggle="tab">Three </a> </li>


          </ul>
          <div class="tab-content">
  <div id="personal" class="tab-pane fade in active">
    <h3>personal</h3>
    <p>Some content.</p>
  </div>
  <div id="promotion" class="tab-pane fade">
    <h3> promotion Menu 1</h3>
    <p>Some content in menu 1.</p>
  </div>
  <div id="vacation" class="tab-pane fade">
    <h3>vacation Menu 2</h3>
    <p>Some content in menu 2.</p>
  </div>
</div>
        </body>
        <script type="text/javascript">
          function ActiveFirstTab() {
       
   
           $('a[href="#personal"]').tab('show');
           
         }
       </script>

     </script>
     </html>