0

I have several links in the menu. Each link is making opacity from 0 to 1 for hidden div. I made dropdown menu this way because I use flex inside that div so I toggle the opacity.

I am using document.getElementsByClassName for selecting an elements. When someone is making a variable with this selector basically he or she has an array with all of the elements with this class.

My code is working, because when I envoke the function from HTML, I'm doing it using parameters. What I would like to do is to connect the link I'm clicking and the div that it shows by index. For example, all links with class A are opening divs with class B. I want to be sure that the first link with class A always opens the first link with class B. I don't want to rely on the parameters in HTML.

How can I do it more efficiently?

function showDropDown(n) {
  let hiddenDiv = document.getElementsByClassName("hidden_dropdown_div");
  hiddenDiv[n].classList.toggle("active");
  for (let i = 0; i < hiddenDiv.length; i++) {
    if (i != n) {
      hiddenDiv[i].classList.remove("active")
    };
  }
};

$(".maindrop_link").click(function(e) {
  e.stopPropagation(); // This is the preferred method.
  return false; // This should not be used unless you do not want
  // any click events registering inside the div
});

$(document).click(function() {
  let hiddenDiv = document.getElementsByClassName("hidden_dropdown_div");
  for (let i = 0; i < hiddenDiv.length; i++) {
    hiddenDiv[i].classList.remove("active");
  }
});
.hidden {
  opacity: 0;
}

.active {
  opacity: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- See the link above the hidden block -->

<li class="dropmenu inlineblock">
  <a class="maindrop_link" href="#" onclick="showDropDown(0)">Makeup</a>
</li>

<div class="hidden_dropdown_div hidden" id="hidden_dropdown_div">

  <div class="hidden_dropdown_link_wrapper">
    <ul class="hidden_dropdown_ul">
      <li><a class="hiddendrop_link" href="#">Wedding Makeup</a></li>
      <li><a class="hiddendrop_link" href="#">Event Makeup</a></li>
      <li><a class="hiddendrop_link" href="#">Creative Makeup</a></li>
    </ul>
  </div>

  <div class="hidden_dropdown_pic_wrapper">

    <div class="item_for_hidden_div">
      <div class="picholder_for_hidden_div"></div>
      <div class="textholder_for_hidden_div"></div>
    </div>

    <div class="item_for_hidden_div">
      <div class="picholder_for_hidden_div"></div>
      <div class="textholder_for_hidden_div"></div>
    </div>

    <div class="item_for_hidden_div">
      <div class="picholder_for_hidden_div"></div>
      <div class="textholder_for_hidden_div"></div>
    </div>

  </div>

</div>
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    Inline event handlers like `onclick` are [bad practice](/q/11737873/4642212). They’re an [obsolete, cumbersome, and unintuitive](/a/43459991/4642212) way to listen for events. Always [use `addEventListener`](//developer.mozilla.org/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. Then, [How to get index number of clicked element from MouseEvent object](/q/62580902/4642212) together with the correct DOM navigation (`.closest("li")`) should help. It’s still not clear what it means for a link to “open” another link. [Edit] to clarify. – Sebastian Simon Mar 06 '22 at 02:10
  • 1
    What do you mean by "more efficiently" exactly? I don't understand the specification or question here. – ggorlen Mar 06 '22 at 05:46
  • https://www.w3schools.com/tags/att_data-.asp this will help you, make special attributes for linking each element in A to one element in B – Osama Mar 06 '22 at 05:50
  • @Osama The community-curated documentation is here: [Using data attributes](//developer.mozilla.org/docs/Learn/HTML/Howto/Use_data_attributes). – Sebastian Simon Mar 06 '22 at 15:05

0 Answers0