chaser7016 chaser7016 - 3 months ago 11
HTML Question

Show Contents of Drop Down

I have code that when clicked expands a drop down to show a set of links. There are three different drop downs each with a different set of links that show when a user clicks.

The script is below, but it is not dynamic. What is a better and more efficient way to write the script, so I do not need to add to it when additional drop downs are needed?



$(".button-nav1").click(function() {
$(".row1").toggle();
});

$(".button-nav2").click(function() {
$(".row2").toggle();
});

$(".button-nav3").click(function() {
$(".row3").toggle();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="row1" class="button-nav1">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row1 BGpadding">
<ul class="2ndRESET">
<li class="resetTWO"><strong>Overview</strong>
</li>
<li><strong>Training</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lorem Lipsum</a>
</li>
</ul>
<li><strong>Toomy</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lipsum </a>
</li>
<li><a href="#">Loremu</a>
</li>
<li><a href="#">Lipsum</a>
</li>
</ul>
</ul>
</div>
</div>

<div id="row2" class="button-nav2">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row2 BGpadding">
<ul class="2ndRESET">
<li class="resetTWO"><strong>Overview</strong>
</li>
<li><strong>Training and Help</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lorem Lipsum</a>
</li>
</ul>
<li><strong>Toomy</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lipsum </a>
</li>
<li><a href="#">Loremu</a>
</li>
<li><a href="#">Lipsum</a>
</li>
</ul>
</ul>
</div>
</div>

<div id="row3" class="button-nav3">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row3 BGpadding">
<ul class="2ndRESET">
<li class="resetTWO"><strong>Overview</strong>
</li>
<li><strong>Training</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lorem Lipsum</a>
</li>
</ul>
<li><strong>Toomy</strong>
</li>
<ul class="resetTWO">
<li><a href="#">Lipsum </a>
</li>
<li><a href="#">Loremu</a>
</li>
<li><a href="#">Lipsum</a>
</li>
</ul>
</ul>
</div>
</div>




Answer

You can reduce your jquery to one function. First listen to divs that have a class that starts with button-nav with the $("div[class^=button-nav]") query. Then get the id and toggle any class with that id with $("."+$(this).attr('id')).toggle();

$("div[class^=button-nav]").on('click',function() {
  $("."+$(this).attr('id')).toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="row1" class="button-nav1">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row1 BGpadding">
  <ul class="2ndRESET">
    <li class="resetTWO"><strong>Overview</strong>
    </li>
    <li><strong>Training</strong>
    </li>
    <ul class="resetTWO">
      <li><a href="#">Lorem Lipsum</a>
      </li>
    </ul>
    <li><strong>Toomy</strong>
    </li>
    <ul class="resetTWO">
      <li><a href="#">Lipsum </a>
      </li>
      <li><a href="#">Loremu</a>
      </li>
      <li><a href="#">Lipsum</a>
      </li>
    </ul>
  </ul>
</div>
</div>

<div id="row2" class="button-nav2">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row2 BGpadding">
  <ul class="2ndRESET">
    <li class="resetTWO"><strong>Overview</strong>
    </li>
    <li><strong>Training and Help</strong>
    </li>
    <ul class="resetTWO">
      <li><a href="#">Lorem Lipsum</a>
      </li>
    </ul>
    <li><strong>Toomy</strong>
    </li>
    <ul class="resetTWO">
      <li><a href="#">Lipsum </a>
      </li>
      <li><a href="#">Loremu</a>
      </li>
      <li><a href="#">Lipsum</a>
      </li>
    </ul>
  </ul>
</div>
</div>

<div id="row3" class="button-nav3">Joe<span class="crt">&nbsp;▼</span>
</div>
<div class="row3 BGpadding">
  <ul class="2ndRESET">
    <li class="resetTWO"><strong>Overview</strong>
    </li>
    <li><strong>Training</strong>
    </li>
    <ul class="resetTWO">
      <li><a href="#">Lorem Lipsum</a>
      </li>
    </ul>
    <li><strong>Toomy</strong>
    </li>
    <ul class="resetTWO">
      <li><a href="#">Lipsum </a>
      </li>
      <li><a href="#">Loremu</a>
      </li>
      <li><a href="#">Lipsum</a>
      </li>
    </ul>
  </ul>
</div>
</div>