jQuery slideDown() is taking precedence over the final element

I'm having trouble sliding a submenu dropdown on my mobile site. The last element is not moving out of the way as expected, and it's expanding over the current selector. Here is the code snippet:

<li class="dropdown">
  <a href="#">Solutions</a>  
  <ul class="dropdown-menu">
    <li><a href="#">ERP</a></li>
    <li><a href="#">Module</a></li>
    <li><a href="#">WMS</a></li>
    <li><a href="#">Sales Tax </a></li>
    <li class="dropdown hasKids">
      <a href="#" class="rd-with-ul">Websites &nbsp;<b class="caret-right"></b><span class="rd-submenu-toggle"></span></a>
      <ul class="dropdown-menu websiteClasses">
        <li><a href="#">A</a></li>
        <li><a href="#">B</a></li>
      </ul>
    </li>
  </ul>
</li>
<li><a href="contactUs.html" class="mobileFont">Contact</a></li>

$(".hasKids").on('click', function(){
  $(this).find(">:first-child").toggleClass('active');
})
$(".hasKids").on('click', function(){
  if ($('.hasKids').find(">:first-child").hasClass("active")){
    console.log('active');
    var submenu = $(".hasKids").find(">:nth-child(2)");
    submenu.slideDown();
  } else {
    console.log('not active');
 }
})

I need help with making the dropdown for "hasKids" slide down to display the two li elements A and B, while pushing the "Contact Us" link out of the way when clicked. It should slide back up when "hasKids" is clicked again. Thank you!

Answer №1

Kindly verify if this code meets your requirements:

$(".hasKids").on('click', function(){
    $(this).children("a").toggleClass('active');
    var submenu = $(this).children(".dropdown-menu");
    if ($(this).children("a").hasClass("active")){
      console.log('active');     
      submenu.slideDown();
    } else {
      console.log('not active');
      submenu.slideUp();
   }
});

I combined the two event handlers and adjusted the selectors to rely on classes and tags instead of position (utilizing >:first-child and >:nth-child selectors).

Answer №2

After working on a solution of my own, I noticed it needs some adjustments when switching between desktop and mobile views. Although I have yet to test your answer, I plan to do so this weekend. Your approach seems much more elegant without the need for child selectors. Thank you!

$(".hasKids").on('click', function(){
            console.log($this);
            var submenu = $(".hasKids").find(">:nth-child(2)");
            if ($('.hasKids').find(">:first-child").hasClass("active")){
                submenu.removeClass();
                submenu.addClass("rd-mobilemenu_submenu");
                submenu.stop().slideDown();
            } else {
                submenu.removeClass("rd-mobilemenu_submenu");
                submenu.stop().slideUp();
                submenu.addClass("dropdown-menu");
            }
        })

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The module 'cropit' is not located within Npm's directory

I'm having trouble with npm recognizing my installation of cropit. In my package.json, I have specified the following lines: "cropit": "^0.1.9", "jquery": "~2.1.1" Could it be that I selected the wrong version? Do the versions of jquery and cropit I ...

xhr.send(params) allows for sending multiple parameters in one request

Hey there, I'm looking for guidance on passing multiple parameters with the correct syntax. Can someone please assist me? function formSubmit() { var name = document.getElementById('name').value; var email = document.getElementById ...

Creating a custom dynamic favicon and title in NextJS

Hello there! I am in the process of creating a web constructor. Currently, my application functions as follows: I verify the URL that the user is visiting (such as localhost:3000) I identify their project name within my web constructor (localhost:3000 -&g ...

Using the ES6 filter method to iterate through a double nested array of objects

Struggling with filtering a nested array of objects and specifically stuck at the filter part. Any idea on how to eliminate one of the marks? this.state = { data: [ { id: 1, name: "Main", subs: [ { id: "jay", ...

Launch a fresh tab using Chrome's extensions feature

Currently, I am in the process of developing a Google extension that will search for selected text on Google when the user clicks "ctrl+alt+x". Here is the mainfest for the extension: { "name": "A jQuery Chrome extension", "version": "0.1", "descri ...

What is the best way to retrieve table field values and display them in a modal box?

Data Table Display <tbody id="batch_view" class = "a-checkbox"> <% @batches.each do |batch| %> <tr> <td class="<%= batch.id %>"><%= batch.date.strftime("%Y-%m-%d") if !batch.date.nil? %></td> <td> ...

The functionality of the AngularJS nested router is not functioning properly

I am encountering some challenges with Angular routing, specifically when using nested routes: The current 'abc' state works flawlessly for the route /admin/team/:teamId .state('admin', { url: '/admin', controller: & ...

Retrieve the outer-HTML of an element when it is clicked

I am working on a project to develop a tool for locating xpath, and I am seeking the most efficient and user-friendly method for allowing the user to select the desired element on a webpage. Ideally, this selection should be made with just a single click, ...

form doesn't transmit data via remote in ajax requests

Trying to set up a chat feature on my website using Rails 4, I followed a tutorial at Encountering an issue when posting a new message in the chat: The form is submitted normally (as HTML) resulting in a missing template error for messages/create. This is ...

JavaScript does not recognize jsPDF

After importing the jsPDF library, I attempted to export to PDF but encountered a JavaScript error stating that jsPDF is not defined. I tried various solutions from similar posts but none of them seemed to work for me. You can find the fiddle here: https ...

Is there a way to display subfolders within a main folder using just one component in React (Next.js)?

English is not my strong suit, so I appreciate your understanding. First things first, please review my code below: const DriveFile = ({folderPk}) => { const [rootFolder, setRootFolder] = useState([]) const viewFolder = async () => { ...

Tips on transferring a numerical ID variable from one page to another by clicking a link

I need help with passing a variable from an auto-generated button to another page when the button is clicked. The buttons all have the same link but different variables. How can I achieve this? Once I generate the button, I assign it a function like so: ...

What do you think is the root cause of the grey streak?

I've implemented an image slideshow on my website: However, I'm facing an issue with a grey line appearing under the annotation. Here is an example: Below is the code snippet: $(function(){ $('#slides').slides({ ...

Modify the color of the text for the initial letter appearing in the sentence

I am currently exploring the possibility of altering the font color of the initial instance of a letter in a div. For example, if the String were 'The text' and I desired to make the color of 'e' yellow, then only the first e would be i ...

Dynamic form name validation in Angular is crucial for ensuring the accuracy and

When it comes to validating a form in Angular, I usually use the ng-submit directive like this: <form name="formName" ng-submit="formName.$valid && submitForm()"></form> This method works well for forms with predefined names that I se ...

"Troubleshooting the issue of nested jQuery Ajax requests failing to execute properly

UPDATE: I'm getting an error saying that my li tag is considered an illegal token. I'm unsure how to resolve this issue as I believed I was appending it within a ul tag I'm tasked with fetching a JSON array of public Github repositories and ...

Issue with IE7 / IE6: jQuery's 'async: false' attribute is not functioning as expected

After developing a straightforward tracking script that logs users' information to a database upon unloading the page, I encountered compatibility issues with IE7 and IE6. While the script functions smoothly on other browsers, errors arise specificall ...

What is the method for calculating table cell widths in HTML?

Take a look at this code snippet here: http://jsfiddle.net/R3AKT/1/. <table class="main"> <colgroup><col class="votes"><col></colgroup> <tr> <td>Votes</td> <td>Comments</td> </tr ...

Horizontalize your lists

Is there a way to make the li elements horizontal in this pagination setup? I've attempted display: inline and display: float, but neither seem to be working for me. <ul class="pagination" style="display:inline; text-align: center; margin:0;">& ...

Creating a dynamic and engaging website using a combination of Flash elements, Div or HTML overlays, and M

In my efforts to overlay HTML over SWF objects, I've experimented with various CSS styles but encountered a few obstacles. Issue on Safari 4.0.3 PC: The SWF object consistently appears above the HTML content. Problem in Firefox 3.5.6 PC: While the ...