javascript - How do I create a show/hide loop in jQuery? -


here html 3 questions , 3 answers:

<div class="faq-carousel"> <div class="all-questions question1">     <h4>question 1</h4> </div> <div class="all-questions question2">     <h4>question 2</h4> </div> <div class="all-questions question3">     <h4>question 3</h4> </div>  <div class=" all-answers answer1">     <p>answer 1</p> </div>               <div class=" all-answers answer2">     <p>answer 2</p> </div>   <div class=" all-answers answer3">     <p>answer 3</p> </div> 

here jquery shows/hides 3 questions , answers:

jquery(document).ready(function () {  "use strict";  jquery(".all-answers").hide(); jquery(".answer1").show(); jquery(".all-questions").removeclass("highlighted"); jquery(".question1").addclass("highlighted");  var slidenumber = 1; jquery(".question1").click(function () {     jquery(".all-answers").hide();     jquery(".answer1").show();     jquery(".all-questions").removeclass("highlighted");     jquery(".question1").addclass("highlighted");     slidenumber = 1; });  jquery(".question2").click(function () {     jquery(".all-answers").hide();     jquery(".answer2").show();     jquery(".all-questions").removeclass("highlighted");     jquery(".question2").addclass("highlighted");     slidenumber = 2; });  jquery(".question3").click(function () {     jquery(".all-answers").hide();     jquery(".answer3").show();     jquery(".all-questions").removeclass("highlighted");     jquery(".question3").addclass("highlighted");     slidenumber = 3; }); }); 

how can change jquery can add more q , a's hmtl without having add more jquery?

many thanks!

the process you're trying achieve here 'dry' code, in other words, don't repeat yourself.

to achieve need can use common classes on questions , answers, relate 2 indexes, this:

"use strict";    jquery(document).ready(function($) {    $('.question').click(function() {      $('.question').removeclass('highlighted');      var index = $(this).addclass('highlighted').index();      $('.answer').hide().eq(index).show();    });  });
.answer { display: block; }  .answer ~ .answer { display: none; }  .highlighted { background-color: #cc0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="faq-carousel">    <div class="question">      <h4>question 1</h4>    </div>    <div class="question">      <h4>question 2</h4>    </div>    <div class="question">      <h4>question 3</h4>    </div>      <div class="answer">      <p>answer 1</p>    </div>    <div class="answer">      <p>answer 2</p>    </div>    <div class="answer">      <p>answer 3</p>    </div>  </div>

alternatively, if want explicitly link elements together, due html structure restrictions example, can use data attributes specify relationships between elements:

"use strict";    jquery(document).ready(function($) {    $('.question').click(function() {      $('.question').removeclass('highlighted');      var target = $(this).addclass('highlighted').data('target');      $('.answer').hide().filter(target).show();    });  });
.answer { display: block; }  .answer ~ .answer { display: none; }  .highlighted { background-color: #cc0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="faq-carousel">    <div class="question" data-target="#answer-01">      <h4>question 1</h4>    </div>    <div class="question" data-target="#answer-02">      <h4>question 2</h4>    </div>    <div class="question" data-target="#answer-03">      <h4>question 3</h4>    </div>      <div class="answer" id="answer-01">      <p>answer 1</p>    </div>    <div class="answer" id="answer-02">      <p>answer 2</p>    </div>    <div class="answer" id="answer-03">      <p>answer 3</p>    </div>   </div>


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -