skip to Main Content

Javascript / jQuery newbie here. This question has been asked numerous times before but I am not able to implement any of the solutions and make them work on my single-page website. My code below. So far, I have tried variations of the following (without success):

Below is the closest I have got to a solution. What am I missing?

$(document).load(function() {
  $('#section-A a').click(function(e) {
    e.preventDefault();
    $('a[href="' + $(this).attr('href') + '"]').tab('show');
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<section id="section-A">

  <blockquote class="blockquote">
    <p>Some text here ...
      <a href="#tab-1">Tab 1 hyperlink text</a> blablabla
      <a href="#tab-2">Tab 2 hyperlink text</a> and blabla
      <a href="#tab-3">Tab 3 hyperlink text</a> and blabla
      <a href="#tab-4">Tab 4 hyperlink text</a> and bla.
    </p>
  </blockquote>

</section>


<div id="section-B">

  <ul class="nav nav-pills">
    <li class="active"><a href="#tab-1" data-toggle="tab">Tab 1 title</a></li>
    <li class=""><a href="#tab-2" data-toggle="tab">Tab 2 title</a></li>
    <li class=""><a href="#tab-3" data-toggle="tab">Tab 3 title</a></li>
    <li class=""><a href="#tab-4" data-toggle="tab">Tab 4 title</a></li>
  </ul>

  <div class="tab-content">
    <div id="tab-1" class="tab-pane active">Tab 1 Lorem ipsum …</div>
    <div id="tab-2" class="tab-pane">Tab 2 Lorem ipsum …</div>
    <div id="tab-3" class="tab-pane">Tab 3 Lorem ipsum …</div>
    <div id="tab-4" class="tab-pane">Tab 4 Lorem ipsum …</div>
  </div>

</div>

2

Answers


  1. Replace $(document).load to $(document).ready

    $(document).ready(function() {
      $('#section-A a').click(function(e) {
        e.preventDefault();
        $('a[href="' + $(this).attr('href') + '"]').tab('show');
      })
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    
    <section id="section-A">
    
      <blockquote class="blockquote">
        <p>Some text here ...
          <a href="#tab-1">Tab 1 hyperlink text</a> blablabla
          <a href="#tab-2">Tab 2 hyperlink text</a> and blabla
          <a href="#tab-3">Tab 3 hyperlink text</a> and blabla
          <a href="#tab-4">Tab 4 hyperlink text</a> and bla.
        </p>
      </blockquote>
    
    </section>
    
    
    <div id="section-B">
    
      <ul class="nav nav-pills">
        <li class="active"><a href="#tab-1" data-toggle="tab">Tab 1 title</a></li>
        <li class=""><a href="#tab-2" data-toggle="tab">Tab 2 title</a></li>
        <li class=""><a href="#tab-3" data-toggle="tab">Tab 3 title</a></li>
        <li class=""><a href="#tab-4" data-toggle="tab">Tab 4 title</a></li>
      </ul>
    
      <div class="tab-content">
        <div id="tab-1" class="tab-pane active">Tab 1 Lorem ipsum …</div>
        <div id="tab-2" class="tab-pane">Tab 2 Lorem ipsum …</div>
        <div id="tab-3" class="tab-pane">Tab 3 Lorem ipsum …</div>
        <div id="tab-4" class="tab-pane">Tab 4 Lorem ipsum …</div>
      </div>
    
    </div>
    Login or Signup to reply.
  2. Just use $(document).ready() instead of load. Checkout this demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search