mpeterson
mpeterson

Reputation: 71

How can you make multiple toggles for one accordion?

I have a product feature map I'm trying to create. I was able to get the accordions working, but how can I make the "+" buttons on the product image also trigger the corresponding accordion?

https://jsfiddle.net/rza4hs16/1/

Here is some of my copied code from the jsfiddle:


<div class="accordion-toggle">
<div class="map-container">
<img src="https://i.imgur.com/quhRUKx.jpg" width="500px">
<a id="map-marker-1" class="map-marker" href="#"><i class="fa fa-plus"></i></a>
</div>
<div class="accordion-container">
  <div class="set">
    <a href="#">
   Accordion 1
      <i class="fa fa-plus"></i>
    </a>
    <div class="content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.</p>
    </div>
  </div>

</div>
</div>

jquery:

$(document).ready(function() {
  $(".set > a").on("click", function() {
    if ($(this).hasClass("active")) {
      $(this).removeClass("active");
      $(this)
        .siblings(".content")
        .slideUp(200);
      $(".set > a i")
        .removeClass("fa-minus")
        .addClass("fa-plus");
    } else {
      $(".set > a i")
        .removeClass("fa-minus")
        .addClass("fa-plus");
      $(this)
        .find("i")
        .removeClass("fa-plus")
        .addClass("fa-minus");
      $(".set > a").removeClass("active");
      $(this).addClass("active");
      $(".content").slideUp(200);
      $(this)
        .siblings(".content")
        .slideDown(200);
    }
  });
});

Upvotes: 0

Views: 426

Answers (2)

Jacob Brazeal
Jacob Brazeal

Reputation: 654

This is a good opportunity to use a data- attribute. You can add user-defined properties to any DOM element this way.

<div class="accordion-toggle">
<div class="map-container">
<img src="https://i.imgur.com/quhRUKx.jpg" width="500px">
<a id="map-marker-1" class="map-marker" href="#" data-feature="1" ><i class="fa fa-plus"></i></a>
</div>
<div class="accordion-container">
  <div class="set" data-feature="1" >
    <a href="#">
   Accordion 1
      <i class="fa fa-plus"></i>
    </a>
    <div class="content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna.</p>
    </div>
  </div>

</div>
</div>

And your JS:

$(".map-marker").click(()=>{
    $('.set[data-feature="' + $(this).attr('data-feature') + '"] > a').click();
});

Also, since it's better not to trigger code by emulating events, it would be good to move the accordion code out of the event handler into a separate function and have both event handlers call it.

Upvotes: 1

tik27
tik27

Reputation: 2698

Add and ID to the accordion

    <a href="#" id="acc1">
         Accordion 1
  <i class="fa fa-plus"></i>
</a>

You can then get the accordion object and call the click command from there from your + button

<a id="map-marker-1" class="map-marker" href="#" onclick='$("#acc1").click()'>

Fiddle: https://jsfiddle.net/ocwus17y/

Upvotes: 1

Related Questions