Multiple dropmenu buttons - Open only clicked button

I found good split dropmenu button script.

Created multiple buttons, and problem is if I click one button then all dropmenus of other buttons open...

So what I want to accomplish is only to open dropmenu that i've clicked not all menus (10post per page so i have 10buttons) that are on page.

Guess I need some id recognition code in my .js.

my script:

  $(function() {
  var splitBtn = $(".x-split-button");
  $("button.x-button-drop").on("click", function() {
    if (!splitBtn.hasClass("open"))
      splitBtn.addClass("open");
  });

  $(".x-split-button").click(function(event) {
    event.stopPropagation();
  });

  $("html").on("click", function() {
    if (splitBtn.hasClass("open"))
      splitBtn.removeClass("open");
  });
});
.x-split-button {
  position: relative;
  display: inline-block;
  text-align: left;
  margin-top: 20px;
}

.x-button {
  position: relative;
  margin: 0;
  height: 30px;
  float: left;
  outline: none;
  font-weight: 600;
  line-height: 27px;
  background: #F2F2F2;
  border: 1px solid #E0E0E0;
  box-shadow: 1px 1px 2px #E0E0E0;
}

.x-button:hover {
  cursor: pointer;
  background: #E0E0E0;
}

.x-button:active {
  background: #D3D3D3;
}

.x-button.x-button-drop {
  border-left: 0;
  height: 30px;
}

.open > .x-button-drop-menu {
  display: block;
}

.x-button-drop-menu {
  position: absolute;
  top: 27px;
  right: 0;
  z-index: 1000;
  display: none;
  float: left;
  min-width: 160px;
  padding: 5px 0;
  margin: 2px 0 0;
  font-size: 14px;
  list-style: none;
  background-color: #F2F2F2;
  color: #000000;
  background-clip: padding-box;
  border: 1px solid #E0E0E0;
  box-shadow: 1px 1px 2px #E0E0E0;
}

.x-button-drop-menu li a {
  display: block;
  padding: 3px 20px;
  clear: both;
  font-family: arial;
  color: #000000;
  text-decoration: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<center><font color="green">1st forum post....text goes here....</font><br/>

<span class="x-split-button">
      <button class="x-button x-button-main">Admin Options</button>
      <button class="x-button x-button-drop">&#9660;</button>
      <ul class="x-button-drop-menu">
        <li>
          <a href="">Edit</a>
        </li>
        <li>
          <a href="">Delete</a>
        </li>
        <li>
          <a href="">Move</a>
        </li>
      </ul>
    </span>
    
    <br/><br/><br/><br/><br/><font color="green">2nd forum post....text goes here....</font><br/>
    <span class="x-split-button">
      <button class="x-button x-button-main">Admin Options</button>
      <button class="x-button x-button-drop">&#9660;</button>
      <ul class="x-button-drop-menu">
        <li>
          <a href="">Edit</a>
        </li>
        <li>
        <a href="">Delete</a>
        </li>
        <li>
          <a href="">Move</a>
        </li>
      </ul>
    </span>
    </center>

1 answer

  • answered 2020-02-12 23:09 Bilel

    jQuery $(this) is concerned about the element concerned in this function (Onclick). We use closest() to reach The parent container (.x-split-button) which is the targeted element in this case.

    We can also use $(this).parent(".x-split-button").addClass("open");

    In other situations, you could use $(this).parents(); when the container is not a direct parent.

    $(function() {
      var splitBtn = $(".x-split-button");
      $("button.x-button-drop").on("click", function() {
        if (!($(this).closest(".x-split-button").hasClass("open"))){
    	splitBtn.removeClass("open");
      $(this).closest(".x-split-button").addClass("open");
    }else{
      $(this).closest(".x-split-button").removeClass("open");
      }});
    
      $(".x-split-button").click(function(event) {
        event.stopPropagation();
      });
    
      $("body").on("click", function() {
        if (splitBtn.hasClass("open"))
          splitBtn.removeClass("open");
      });
    });
    .x-split-button {
      position: relative;
      display: inline-block;
      text-align: left;
      margin-top: 20px;
    }
    
    .x-button {
      position: relative;
      margin: 0;
      height: 30px;
      float: left;
      outline: none;
      font-weight: 600;
      line-height: 27px;
      background: #F2F2F2;
      border: 1px solid #E0E0E0;
      box-shadow: 1px 1px 2px #E0E0E0;
    }
    
    .x-button:hover {
      cursor: pointer;
      background: #E0E0E0;
    }
    
    .x-button:active {
      background: #D3D3D3;
    }
    
    .x-button.x-button-drop {
      border-left: 0;
      height: 30px;
    }
    
    .open > .x-button-drop-menu {
      display: block;
    }
    
    .x-button-drop-menu {
      position: absolute;
      top: 27px;
      right: 0;
      z-index: 1000;
      display: none;
      float: left;
      min-width: 160px;
      padding: 5px 0;
      margin: 2px 0 0;
      font-size: 14px;
      list-style: none;
      background-color: #F2F2F2;
      color: #000000;
      background-clip: padding-box;
      border: 1px solid #E0E0E0;
      box-shadow: 1px 1px 2px #E0E0E0;
    }
    
    .x-button-drop-menu li a {
      display: block;
      padding: 3px 20px;
      clear: both;
      font-family: arial;
      color: #000000;
      text-decoration: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <center><font color="green">1st forum post....text goes here....</font><br/>
    
    <span class="x-split-button">
          <button class="x-button x-button-main">Admin Options</button>
          <button class="x-button x-button-drop">&#9660;</button>
          <ul class="x-button-drop-menu">
            <li>
              <a href="">Edit</a>
            </li>
            <li>
              <a href="">Delete</a>
            </li>
            <li>
              <a href="">Move</a>
            </li>
          </ul>
        </span>
        
        <br/><br/><br/><br/><br/><font color="green">2nd forum post....text goes here....</font><br/>
        <span class="x-split-button">
          <button class="x-button x-button-main">Admin Options</button>
          <button class="x-button x-button-drop">&#9660;</button>
          <ul class="x-button-drop-menu">
            <li>
              <a href="">Edit</a>
            </li>
            <li>
            <a href="">Delete</a>
            </li>
            <li>
              <a href="">Move</a>
            </li>
          </ul>
        </span>
        </center>