kate moss
kate moss

Reputation: 786

How to redirect to another div while clicking on accordion elements?

Whenever user click on default1 tab it will open default2 and when again click on default1 while default2 is active it will show default1 accordions.

Now I am trying whenever user click on any accordion element of default1 i.e. 1 and 2, as show in image it will also redirect to default2 tab

click on any area of accordion 1 or 2 element it should redirect to default2 tab!

enter image description here

p.s- not only on 2 user can click anywhere on the accordion and it will redirect to tab2 i.e. default2

// TAB Buttons
const tab1 = document.querySelector('#tab11')
const tab2 = document.querySelector('#tab12')

// TAB Content
const allTabPane = document.querySelectorAll('.tab-pane')

tab1.addEventListener('click', toggleTabs);
tab2.addEventListener('click', toggleTabs);

function clickTab(tab) {
    if (tab.parentNode.classList.contains('active') === false) tab.click();
}

function toggleTabs(e) {
  e.preventDefault();
  if (this.parentNode.classList.contains('active')) {
      let toClick = this.parentNode.parentNode.querySelector('li:not(.active) > a');
      setTimeout(function() {
          toClick.click();
      }, 40);
  }
}
.pg-content p {
  font-family: poppins;
  font-size: 14px;
  margin-bottom: 0px;
}

.acc-head {
  display: flex;
  justify-content: space-between;
  flex-direction: row-reverse;
  align-items: center;
}

.tab-accrd a {
  text-decoration: none;
  color: #000;
  font-family: poppins;
}

.tab-accrd a i {
  color: #FC453E;
}

.tab-accrd .card {
  background: #eee;
  border-bottom: 1px solid #FC453E;
  border-radius: 0px;
  position: relative;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  background-clip: border-box;
  border: 1px solid rgba(0, 0, 0, .125);
}

.tab-accrd .card-header {
  padding: 0.75rem 1.25rem;
  margin-bottom: 0;
  background-color: rgba(0, 0, 0, .03);
  border-bottom: 1px solid rgba(0, 0, 0, .125);
}

.tab-accrd .card-header:first-child {
  border-radius: calc(0.25rem - 1px) calc(0.25rem - 1px) 0 0;
}

.tab-accrd h5 {
  font-size: 20px;
}

.p-0 {
  padding: 0px;
}

.nav-tabs {
  border: none;
}

.ml-1,
.mx-1 {
  margin-left: 0.25rem!important;
}

.accordion-head i {
  float: right;
}

.accordion-head>.collapsed>i:before {
  content: "\f151";
}

html {
  scroll-behavior: smooth;
}

.tab-accrd span {
  font-size: 30px;
  font-weight: 600;
  color: #fc453e;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div class="col-lg-12 mt-5 tab-accrd">
  <div class="panel with-nav-tabs panel-default">
    <div class="panel-heading">
      <ul class="nav nav-tabs">
        <!-- define id for tabs tab11 and tab12 -->
        <li class="active"><a href="#tab3default" id="tab11" data-toggle="tab">Default 1</a></li>
        <li><a href="#tab4default" id="tab12" data-toggle="tab">Default 2</a></li>
      </ul>
    </div>
    <div class="panel-body">
      <div class="tab-content">
        <div class="tab-pane fade in active" id="tab3default">
          <div class="panel-group" id="faqAccordion">
            <div class="panel panel-default ">
              <div class="panel-heading accordion-toggle question-toggle collapsed" style="padding: 20px;background: #eee;" data-toggle="collapse" data-parent="#faqAccordion" data-target="#question5">
                <div class="d-flex" style="align-items:center;">
                  <span onclick="clickTab(tab1)">1</span>
                  <h5 class="panel-title ml-3">
                    What is Lorem Ipsum?
                  </h5>
                </div>

              </div>
              <div id="question5" class="panel-collapse collapse" style="height: 0px;">
                <div class="panel-body" style="padding: 15px;
                            background: #eee;">
                  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown

                  </p>

                </div>
              </div>
            </div>
            <div class="panel panel-default ">
              <div class="panel-heading accordion-toggle collapsed question-toggle" style="padding: 20px;background: #eee;" data-toggle="collapse" data-parent="#faqAccordion" data-target="#question6">
                <div class="d-flex" style="align-items:center;">
                  <span onclick="clickTab(tab2)">2</span>
                  <h5 class="panel-title ml-3">
                    What is Lorem Ipsum?
                  </h5>
                </div>

              </div>
              <div id="question6" class="panel-collapse collapse" style="height: 0px;">
                <div class="panel-body" style="padding: 15px;
                          background: #eee;">
                  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
                  </p>

                </div>
              </div>
            </div>

          </div>
        </div>
        <div class="tab-pane fade" id="tab4default">
          <div class="panel-group" id="faqAccordion">
            <div class="panel panel-default ">
              <div class="panel-heading accordion-toggle question-toggle " style="padding: 20px;background: #eee;" data-toggle="collapse" data-parent="#faqAccordion" data-target="#question9">
                <div class="d-flex" style="align-items:center;">
                  <span onclick="clickTab(tab1)">1</span>
                  <h5 class="panel-title ml-3">
                    What is Lorem Ipsum?
                  </h5>
                </div>

              </div>
              <div id="question9" class="panel-collapse" style="height: 0px;">
                <div class="panel-body" style="padding: 15px;
                            background: #eee;">
                  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the
                  </p>

                </div>
              </div>
            </div>
            <div class="panel panel-default ">
              <div class="panel-heading accordion-toggle collapsed question-toggle" style="padding: 20px;background: #eee;" data-toggle="collapse" data-parent="#faqAccordion" data-target="#question1">
                <div class="d-flex" style="align-items:center;">
                  <span onclick="clickTab(tab2)">2</span>
                  <h5 class="panel-title ml-3">
                    What is Lorem Ipsum?
                  </h5>
                </div>

              </div>
              <div id="question1" class="panel-collapse" style="height: 0px;">
                <div class="panel-body" style="padding: 15px;
                          background: #eee;">
                  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
                  </p>

                </div>
              </div>
            </div>

          </div>
        </div>
      </div>
    </div>
  </div>

Upvotes: 0

Views: 587

Answers (1)

Lucretius
Lucretius

Reputation: 1069

Seems to be working fine in this fiddle even with the clickTab() and toggleTabs() functions disabled, the only change I made was I made sure to load the javascript libraries last at the bottom of the body to make sure the dom elements are loaded before they start working their magic.

<!DOCTYPE html>
<html>
  <head>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  </head>
  <body>
     <!-- all of your content -->
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  </body>
</html>

Also it looks like in your code you have non-unique ID's that will need to be modified, otherwise results will be unexpected.

https://jsfiddle.net/te28cru4/

Update:

https://jsfiddle.net/te28cru4/10/

Here are the changes:

enter image description here

  • The panel group for tabs "Default 1" and "Default 2" are duplicate IDs called id="faqAccordian". ID's must unique, so I renamed the ID for tab group "Default 2" to id="faqCollapse2" and left "Default 1" as is. This allows the "collapse" feature to be separated on each tab.
  • the <div> elements on the "Default 2" page that were set for data-parent="#faqAccordian" were updated to data-parent="#faqAccordian2" to keep the separation consistent.
  • The <div> elements on the "Default 2" tab for id="question9" and id=question1 did not have the "collapse" class, so when clicking into that tab both were initially expanded. I added the collapse class to both.
  • I deleted all of your javascript because I don't think its needed. Accordian functionality is handled natively in the boostrap libraries.
  • Again, I moved the javascript libraries to the bottom of the <body> tag to allow the dom to load.
  • Added some <a> anchor tags in each content section of each according that serves as convenience navigation between the two tabs.
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  </head>
  <body>
    <div class="col-lg-12 mt-5 tab-accrd">
      <div class="panel with-nav-tabs panel-default">
        <div class="panel-heading">
          <ul class="nav nav-tabs">
            <!-- define id for tabs tab11 and tab12 -->
            <li class="active"><a href="#tab3default" id="tab11" data-toggle="tab">Default 1</a></li>
            <li><a href="#tab4default" id="tab12" data-toggle="tab">Default 2</a></li>
          </ul>
        </div>
        <div class="panel-body">
          <div class="tab-content">
            <div class="tab-pane fade in active" id="tab3default">
              <div class="panel-group" id="faqAccordion">
                <div class="panel panel-default">
                  <div class="panel-heading accordion-toggle question-toggle collapsed" style="padding: 20px;background: #eee;" data-toggle="collapse" data-parent="#faqAccordion" data-target="#question5">
                    <div class="d-flex" style="align-items:center;">
                      <span onclick="clickTab(tab1)">Tab 1-1</span>
                      <h5 class="panel-title ml-3">
                        What is Lorem Ipsum?
                      </h5>
                    </div>
                  </div>
                  <div id="question5" class="panel-collapse collapse" style="height: 0px;">
                    <div class="panel-body" style="padding: 15px;
                            background: #eee;">
                      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown

                      </p>
                      <a href="#" class="toTab2">Tab 2</a>
                    </div>
                  </div>
                </div>
                <div class="panel panel-default ">
                  <div class="panel-heading accordion-toggle collapsed question-toggle" style="padding: 20px;background: #eee;" data-toggle="collapse" data-parent="#faqAccordion" data-target="#question6">
                    <div class="d-flex" style="align-items:center;">
                      <span onclick="clickTab(tab2)">Tab 1-2</span>
                      <h5 class="panel-title ml-3">
                        What is Lorem Ipsum?
                      </h5>
                    </div>
                  </div>
                  <div id="question6" class="panel-collapse collapse" style="height: 0px;">
                    <div class="panel-body" style="padding: 15px;
                          background: #eee;">
                      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
                      </p>
                      <a href="#" class="toTab2">Tab 2</a>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="tab-pane fade" id="tab4default">
              <div class="panel-group" id="faqAccordion2">
                <div class="panel panel-default ">
                  <div class="panel-heading accordion-toggle question-toggle collapsed" style="padding: 20px;background: #eee;" data-toggle="collapse" data-parent="#faqAccordion2" data-target="#question9">
                    <div class="d-flex" style="align-items:center;">
                      <span onclick="clickTab(tab1)">Tab 2-1</span>
                      <h5 class="panel-title ml-3">
                        What is Lorem Ipsum?
                      </h5>
                    </div>
                  </div>
                  <div id="question9" class="panel-collapse collapse" style="height: 0px;">
                    <div class="panel-body" style="padding: 15px;
                            background: #eee;">
                      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the
                      </p>
                      <a href="#" class="toTab1">Tab 1</a>
                    </div>
                  </div>
                </div>
                <div class="panel panel-default ">
                  <div class="panel-heading accordion-toggle collapsed question-toggle" style="padding: 20px;background: #eee;" data-toggle="collapse" data-parent="#faqAccordion2" data-target="#question1">
                    <div class="d-flex" style="align-items:center;">
                      <span onclick="clickTab(tab2)">Tab 2-2</span>
                      <h5 class="panel-title ml-3">
                        What is Lorem Ipsum?
                      </h5>
                    </div>

                  </div>
                  <div id="question1" class="panel-collapse collapse" style="height: 0px;">
                    <div class="panel-body" style="padding: 15px;
                          background: #eee;">
                      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
                      </p>
                      <p>
                      <a href="#" class="toTab1">Tab 1</a>
                      </p>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  </body>
</html>
$(document).on('click', '.toTab1', function(e){
        e.preventDefault();
        $('#tab11').trigger('click');
})

$(document).on('click', '.toTab2', function(e){
        e.preventDefault();
        $('#tab12').trigger('click');
})

Update 2:

From clarification in the comments, accordions are not meant for navigation and that by using them for navigation the "content" piece of the expanded accordion will never be seen by the user. Accordions are for compressing content vertically.

enter image description here

What you probably want are html buttons, you can style them as wide and as big as you like.

https://www.w3schools.com/tags/tag_button.asp

Upvotes: 1

Related Questions