Click Event triggers automatically without any manual intervention

I am currently facing an issue with the following code snippet

$(".menu").on('click', function(e) {
    e.preventDefault();
    $(this).addClass("open").children("div").slideDown(200);
});

$(document).on('click', ".menu.open", function(e) {
    e.preventDefault();
    $(this).removeClass("open").children("div").slideUp(200);
});

Whenever I click on the .menu element, the inner div slides down but immediately slides up again and the open class is removed. This happens upon a single click. How can I resolve this issue and make it function properly as a drop-down menu for small screens - opened by a click and closed by another click?

Answer №1

You have made a change to the code snippet by adding the class open upon click and then immediately sliding down-

$(".menu").on('click',function(e){
    $(this).addClass("open").........slideDown(200);
});

This has resulted in the delegated callback being executed. To prevent this, it is suggested to add the class at the end of the animation and ensure that the open menu is not triggered again -

$(".menu").on('click',function(e){
    var tthis = this; //saving the instance for reference in the callback
    if ($(tthis).hasClass('open')){ //ignore if already open
         return;
    }
    $(tthis).children("div").slideDown(200, function(){
        $(tthis).addClass("open"); // add class after the animation
    });
});

$(document).on('click',".menu.open",function(e){
    var tthis = this; //saving the instance for reference in the callback
    $(tthis).children("div").slideUp(200, function(){
        $(tthis).removeClass("open"); // remove class after the animation
    });
});

Answer №2

insert

e.stopPropagation(); 

within each

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

Fixed position halting in a specific area

I have a fiddle where the black box is fixed on the page, but it overlaps the map when scrolling. I want to stop the black box from overlapping the map. How can I achieve this? CSS: .item{ background:#eee; padding:10px; width:50%; margin-bottom:15px;} .n ...

Having trouble retrieving the post ID in WordPress

I am facing an issue with the post id not displaying correctly. Within my jQuery script, when the post id is 1234 it appears as postid:1290. Below is the code snippet calling my action in jQuery. $("#vote").not(".disabled").click(function() { var el ...

Utilizing jQuery grep() to sort through a JSON array

Despite my attempts to find a suitable example on this platform, I am still struggling to adapt them to my specific needs. Essentially, all I require is to filter some JSON results using the grep() function. Here is the JSON data that I am working with: ...

What is the process for sending the selected checkbox text to an action result on a Razor page?

Working with asp.net core razor pages, I am passing the selected id as an array successfully. However, the issue arises when the selected text value for the checkbox is not being passed or displayed. The problem seems to be occurring on this line of code ...

Issues arise when attempting to manipulate the DOM with ng-view in AngularJS

Apologies for not providing the code due to its length. I have a simple application with a single module, controller, and ng-view/routProvider. In my controller, when I use console.log(angular.element('div').length), it correctly shows that ther ...

Trigger the "onChange" event when the input element is modified by JavaScript within a popup window

On my webpage, I have a form element and a popup window (which is opened using window.open). Both the form element and the popup window utilize jQuery. The JavaScript in the popup window has the ability to alter the form element on the main page successf ...

Is it possible to include multiple src URLs for the same image in an HTML document?

Is it possible to include multiple src URLs for one image in HTML? ...

Shuffle contents of a Div randomly

I'm currently in the process of developing a new website and I need to randomly move the news feed. To achieve this, I have created an array containing the list of news items. The array is then shuffled and placed within the news feed section. Althou ...

Struggling to add custom styles to an Ionic radio button

I'm struggling to adjust the position of the icon in an Ionic radio button so that it sits a bit higher, but I can't seem to figure out how to do it. Below is the code snippet for reference: // HTML <ion-radio class="radio-input" mo ...

Determine the width of the uploaded image upon completion of the upload process with the help of the

After uploading an image using uploadify, I need to determine its width so that I can ensure it maintains the correct proportions with the previous one. OBJECTIVE: Determine the WIDTH of the uploaded image ...

The Bootstrap v5 dropdown feature is malfunctioning as the drop-down menu fails to appear

I've been struggling to create a dropdown menu using Bootstrap, but it doesn't seem to be working as expected. Despite that, I have no issues utilizing the framework for styling purposes. The Bootstrap js link is placed at the bottom of the body ...

Interacting with a Bootstrap menu

I recently incorporated an example from Bootstrap, found at http://getbootstrap.com/examples/starter-template/, into my project and it worked well. However, after adding a custom left menu that appears on left-click, I noticed that the color of the Boots ...

Add a border to the progress bar that has no content

I have configured a Bootstrap Progress Bar with the following code snippet: <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax=& ...

Using jQuery to animate two images simultaneously in opposite directions

Trying to make two images animate using a single function. The issue is that one image needs to animate the left attribute while the other needs to animate the right. Here's what I have so far: $(document).ready(function(){ v ...

Unveiling the Secrets of Unearthing Data from JSON

My code receives JSON strings through a JSONP call. Although I'm aware of the general JSON structure, I don't know the specific keys and values it will contain. The typical structure is as follows: [ {"key_name": "value"}, {"key_name": ...

Arranging a list using Flexbox with equal spacing between elements in each row containing three items

I'm struggling with aligning a flexbox list. I have a row of 3 elements, but when there are only 2 elements on the row, there's a gap in the middle. My goal is to have a row with 2 elements look like x x o. However, my current code displays the ...

Utilizing ajax/jquery for form verification in Joomla

I am interested in developing custom form validation on Joomla using either ajax or jQuery. When a user inputs something invalid and moves away from the input field, it should be validated and display an error message. Additionally, all error messages wi ...

I am having trouble assigning the correct z-index to either the id or class of the Facebook message icon, which is appearing behind the menu

Recently, I encountered an issue on my website where the Facebook like button opening a comment window appears behind my drop-down menu. Despite having a z-index of 1000 on the drop menu, I haven't been able to identify which div or class should have ...

Restart Variable Counter following a new AJAX call in JavaScript/jQuery

I am facing an issue with a page where contents are loaded using AJAX. The main page (index.html) has the following code: <a href="#" id="loadContent">Load Remote Page</a> <div id="ajaxContent"> // Loads AJAX contents Here!! </div& ...

PHP results can be manipulated with jQuery and JavaScript

I have a unique jQuery script that is designed to insert custom content after each paragraph in news articles. Here's an example: $(document).ready(function() { $("<a>The link inserted in jQuery</a>") .insertAfter("p") ...