javascript - Jquery event is fired for all button having same class name rather than current selected button -


basically when click on first .menu-btn should called first .dropdown please note content generated on fly, can't use different ids all

$(document).ready(function() {    $(".menu-btn").click(function() {      $(".dropdown").toggleclass('expand');    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <table class="table table-striped">    <tr>      <th>&nbsp;</th>      <th>col 1</th>      <th>col 2</th>      <th>col 3</th>    </tr>    <tr>      <td>        <div class="mobile-nav" style="border:0px solid red">          <div class="menu-btn" id="menu-btn">            <div></div>            <span></span>            <span></span>            <span></span>          </div>            <div class="dropdown">            <ul class="list">              <li>                <button>btn1</button>              </li>              <li>                <button>btn2</button>              </li>              <li>                <button>btn3</button>              </li>              <li>                <button>btn4</button>              </li>            </ul>          </div>        </div>      </td>      <td>lorem ipsum</td>      <td>lorem ipsum</td>      <td>lorem ipsum</td>    </tr>    <tr>      <td>        <div class="mobile-nav" style="border:0px solid red">          <div class="menu-btn" id="menu-btn">            <div></div>            <span></span>            <span></span>            <span></span>          </div>            <div class="dropdown">            <ul class="list">              <li>                <button>btn1</button>              </li>              <li>                <button>btn2</button>              </li>              <li>                <button>btn3</button>              </li>              <li>                <button>btn4</button>              </li>            </ul>          </div>        </div>      </td>      <td>lorem ipsum</td>      <td>lorem ipsum</td>      <td>lorem ipsum</td>    </tr>    <tr>      <td>        <div class="mobile-nav" style="border:0px solid red">          <div class="menu-btn" id="menu-btn">            <div></div>            <span></span>            <span></span>            <span></span>          </div>            <div class="dropdown">            <ul class="list">              <li>                <button>btn1</button>              </li>              <li>                <button>btn2</button>              </li>              <li>                <button>btn3</button>              </li>              <li>                <button>btn4</button>              </li>            </ul>          </div>        </div>      </td>      <td>lorem ipsum</td>      <td>lorem ipsum</td>      <td>lorem ipsum</td>    </tr>  </table>

you can use siblings method target immediate .drop-down

$(document).ready(function(){             $(".menu-btn").click(function(){                 $(this).siblings(".dropdown").toggleclass('expand');             });       }); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -