How do I get my jQuery code to work on all elements


Brown

I have this accordion menu but it only works on the first menu ul. How can I make it work in all ulsuch environments ? I know it would be great in the future if you could explain what I'm doing wrong.

Also, how do I get the link to switch between the two classes rightand on downeach click ? It also has classes on it turq-font. I want it to be rightremoved and replaced with when the link is clicked and the menu is displayed down. Here is my code:

$(function() {
  $("#show-menu").click(function() {
    $("#sub-menu-acc").toggleClass("active-menu", 1000);
  });
});
.active-menu {
  display: block !important;
}
.admin-area ul li {
  margin: 6px 0px;
}
.admin-right-menu ul li a {
  color: black;
  text-decoration: none;
}
.admin-area ul {
  list-style: none;
  margin-bottom: 20px;
}
.admin-area ul li ul {
  display: none;
  padding-left: 20px;
  margin-bottom: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="admin-area">
<div class="admin-right-menu">
  <ul>
    <li><a href="#" id="show-menu" class="turq-font right">Stats ></a>
      <ul id="sub-menu-acc">
        <li><a href="#">Business Stats</a>
        </li>
        <li><a href="#">Affiliate stats</a>
        </li>
      </ul>
    </li>
    <li><a href="#" id="show-menu" class="turq-font right">Reports ></a>
      <ul id="sub-menu-acc">
        <li><a href="#">Global</a>
        </li>
        <li><a href="#">Sales</a>
        </li>
        <li><a href="#">Sales trends</a>
        </li>
      </ul>
    </li>
  </ul>
</div></div>

Rory McCrossan

The problem is due to your use of duplicate idproperties; they should be unique within the scope document. Convert your code to use classes.

Also note that you should thistraverse the DOM using keywords to find content related to the ulclicked aelement and add right downin the toggleClass()call. Try this:

$(function() {
  $(".show-menu").click(function(e) {
    e.preventDefault(); // stop the '#' of the clicked a appearing in the URL 
    $(this).toggleClass('right down').next(".sub-menu-acc").toggleClass("active-menu");
  });
});
.active-menu {
  display: block !important;
}
.admin-area ul li {
  margin: 6px 0px;
}
.admin-right-menu ul li a {
  color: black;
  text-decoration: none;
}
.admin-area ul {
  list-style: none;
  margin-bottom: 20px;
}
.admin-area ul li ul {
  display: none;
  padding-left: 20px;
  margin-bottom: 0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="admin-area">
  <ul>
    <li>
      <a href="#" class="show-menu turq-font right">Stats ></a>
      <ul class="sub-menu-acc">
        <li>
          <a href="#">Business Stats</a>
        </li>
        <li>
          <a href="#">Affiliate stats</a>
        </li>
      </ul>
    </li>
    <li>
      <a href="#" class="show-menu turq-font right">Reports ></a>
      <ul class="sub-menu-acc">
        <li>
          <a href="#">Global</a>
        </li>
        <li>
          <a href="#">Sales</a>
        </li>
        <li>
          <a href="#">Sales trends</a>
        </li>
      </ul>
    </li>
  </ul>
</div>

Related


How do I get my jQuery code to work on all elements

Brown I have this accordion menu but it only works on the first menu ul. How can I make it work in all ulsuch environments ? I know it would be great in the future if you could explain what I'm doing wrong. Also, how do I get the link to switch between the two

How do I get autocomplete (GoCode) to work with all my imports?

Paul: 在GoClipse中安装GoCode自动完成守护程序后,它可用于更一般的导入(fmt等),但不适用于更具体的导入。 我确实相信GoClipse的设置正确,因为它已经可以用于某些导入。我要使其起作用的特定导入是“ github.com/hyperledger/fabric/core/chaincode/shim”。 要使这些导入正常工作,必须做一些事情,但我还没有弄清楚。我总是可以在没有自动补全的情况下进行编码,但是。 有谁知道如何使它工作?谢谢你 注意:我会发布图片来说明我的问题,但是很好:“发布图片至

How do I get autocomplete (GoCode) to work with all my imports?

Paul: After installing the GoCode autocomplete daemon in GoClipse, it works for more general imports (fmt, etc.), but not for more specific imports. I do believe that GoClipse is set up correctly as it already works for some imports. The specific import I'm tr

How do I get autocomplete (GoCode) to work with all my imports?

Paul: After installing the GoCode autocomplete daemon in GoClipse, it works for more general imports (fmt, etc.), but not for more specific imports. I do believe that GoClipse is set up correctly as it already works for some imports. The specific import I'm tr

How do I get my nested if statement to work in jQuery

user3298548 Right now there is an input text box where users type in a word. Once they type in that word and press enter, the word disappears and a new one appears and so on. If they type it wrong the color turns red. I can only get it to work once however bec

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my $_Post to work

turquoise I'm trying to get my site to submit an email every time someone sends a message (now I'm struggling to get variables to work...). It's a learning process, so I'm giving it my all here, just trying to teach myself. But don't like global php commands $

How do I get my ListBox to work

Niermag I'm trying to get a listbox to work that will function very much like this. These Availableexercises are derived from Migration/Configuration. I need to be able to add multiples Availableto each exercise Selected Regime. I'm using viewmodels to access

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get my ListBox to work

Niermag I'm trying to get a listbox to work that will function very much like this. These Availableexercises are derived from Migration/Configuration. I need to be able to add multiples Availableto each exercise Selected Regime. I'm using viewmodels to access

How do I get this C code to work?

Caesar 381 My knowledge is limited so bear with me, but I'm trying to create functions and this is what I've got so far and it's not. Thanks for all suggestions! Try creating a function that collects user input #include<stdio.h> int GetIntFromUser(int grades[

How can I implement my jQuery code to work on my website?

sand witch I am new to jQuery. I would like to implement 2 short jQuery lines of code in my javascript file to style the item borders displayed after a search. But it doesn't work. Do I have to include the jQuery library again in the js file or is it enough if

How can I implement my jQuery code to work on my website?

sand witch I am new to jQuery. I would like to implement 2 short jQuery lines of code in my javascript file to style the item borders displayed after a search. But it doesn't work. Do I have to include the jQuery library again in the js file or is it enough if

How can I implement my jQuery code to work on my website?

sand witch I am new to jQuery. I would like to implement 2 short jQuery lines of code in my javascript file to style the item borders displayed after a search. But it doesn't work. Do I have to include the jQuery library again in the js file or is it enough if

How can I implement my jQuery code to work on my website?

sand witch I am new to jQuery. I would like to implement 2 short jQuery lines of code in my javascript file to style the item borders displayed after a search. But it doesn't work. Do I have to include the jQuery library again in the js file or is it enough if

How can I implement my jQuery code to work on my website?

sand witch I am new to jQuery. I would like to implement 2 short jQuery lines of code in my javascript file to style the item borders displayed after a search. But it doesn't work. Do I have to include the jQuery library again in the js file or is it enough if

How do I get jQuery to work with me?

Denislav Karagizov I am making an accordion. Watch a guy on youtube do this and write everything his way and it works on his video but not on my computer. Here's what I do (yes, I do declare the HTML at the beginning, and yes, I have the HTML turned on and off

How do I get jQuery to work with me?

Denislav Karagizov I am making an accordion. Watch a guy on youtube do this and write everything his way and it works on his video but not on my computer. Here's what I do (yes, I do declare the HTML at the beginning, and yes, I have the HTML turned on and off