My objective is to close a menu that is currently open by clicking on the child element of the parent

My jQuery code is functioning properly, but I am aiming to have the child ul close only when I specifically click on it; currently, it closes when I click on the parent li as well. How can I make this adjustment to the behavior?

http://jsfiddle.net/c4k5w/2/

$(document).ready(function(){   

$("ul>li").click(function(){
    var lk=$(this).text();
    var w=$("ul li:contains('"+lk+"')").width();
    $("ul li").removeClass("h");
    $("ul li:contains('"+lk+"')").addClass("h");
    if(w>200){var a=1;$("ul li").removeClass("h");}else{var a=0;}       
    if(a==1){
        $("ul li:contains('"+lk+"') ul").hide(function(){
        $("ul li:contains('"+lk+"')").animate({width:"200px"},"fast");      
        });         
    }else{  
            $("ul li ul").hide();
            $("ul>li").animate({width:"200px"},"fast");
            $("ul li:contains('"+lk+"')").animate({width:"1200px"},"fast",function(){
            $("ul li:contains('"+lk+"')>ul").slideDown();
        });
    }
});

});

Answer №1

To prevent the click event from firing for child elements of ul, you can use the following code:

$(document).ready(function(){       
    $("ul>li").click(function(){
        var lk=$(this).text();
        var w=$("ul li:contains('"+lk+"')").width();
        $("ul li").removeClass("h");
        $("ul li:contains('"+lk+"')").addClass("h");
        if(w>200){var a=1;$("ul li").removeClass("h");}else{var a=0;}       
        if(a==1){
            $("ul li:contains('"+lk+"') ul").hide(function(){
                $("ul li:contains('"+lk+"')").animate({width:"200px"},"fast");      
            });         
        }else{  
            $("ul li ul").hide();
            $("ul>li").animate({width:"200px"},"fast");
            $("ul li:contains('"+lk+"')").animate({width:"1200px"},"fast",function(){
                $("ul li:contains('"+lk+"')>ul").slideDown();
            });
        }
    }).children().click(function(e) {
      return false;
    });

});

View Demo

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

Parsing JSON data using multilevel iteration in JavaScript is a complex yet

{ "id":0, "item":[ { "id":"0-", "text":"BlueWing", "userdata":[ { "name":"cid", "content":"10377" } ], "item":[ { "id" ...

If a certain condition is met within an ng-repeat loop on a particular variable

When obtaining information in angularjs from an SQL query, it may appear as follows: {"orders":[ { "orderId":"1674","itemId":"468","SubTotal":"40.00","Total":"48.98","Status":"Printed","Title":"Mangled : Large","Quantity":"1","dateCreated":"2009-06-03 ...

Disabling a specific tab in an array of tabs using Angular and Typescript

Displayed below are 5 tabs that can be clicked by the user. My goal is to disable tabs 2 and 3, meaning that the tab names will still be visible but users will not be able to click on them. I attempted to set the tabs to active: false in the TypeScript fi ...

Ways to retrieve all Exit values from an Array

console.log(data); output: { "Status": "OK", "Message": "", "Data": { "LocationId": 1, "LocationName": null, "LocationData": [ ], "DeviceData": [ ], "AverageData": [ { "Timestamp": "2017-01-01T00:00:00" ...

Obtain the name of the checkbox that has been selected

I'm new to JavaScript and HTML, so my question might seem silly to you, but I'm stuck on it. I'm trying to get the name of the selected checkbox. Here's the code I've been working with: <br> <% for(var i = 0; i < ...

JavaScript - Attempting to Add Objects to Array Unsuccessful

After seeing this question raised multiple times, I am determined to find a solution. In my current project, I am tasked with displaying a list of orders and implementing a filter by date functionality. However, I keep encountering an error when trying to ...

How can you create a vertical bar graph using CSS that fills in from the bottom to the top instead of top to bottom?

I have created a bar graph using HTML and CSS. The issue I am facing is that the bar fills up in reverse order - when the height is set to 0%, the bar appears filled at 100%. I attempted changing the CSS property from "top" to "bottom," but it did not reso ...

What is the best way to click on a specific webElement in a group of webElements using Python Selenium?

After discovering how to locate a subelement of an element using Selenium in python through this informative Stack Overflow post, I am now eager to take my skills to the next level. Selenium Get WebElement inside a WebElement My goal is to be able to exe ...

Utilizing AJAX to submit an array of form inputs and securely storing the data in MySQL using

I have a set of code below that has been functioning well for a single input field. However, I also have additional code that adds extra fields to the form when a div with id #addForm is clicked, and these inputs are named "itemName[]". <script> ...

Sorting data in Javascript can be done efficiently by utilizing the .filter method

Can someone help me identify what I might be doing incorrectly? I have a chained filter under computed that is giving me an error message stating 'product.topic.sort' is not a function. My intention is to use 'select' to provide sortin ...

What causes the variation in `this` when viewing index.html in a browser versus supplying index.html through a node server?

My html file named index.html is referencing a javascript file <!DOCTYPE html> <html lang="en"> <head> <title>asd</title> <meta charset="utf-8"> </head> <body> <div id="app"></div> ...

Issue encountered in protobuf | npm ERR! code ELIFECYCLE: encountered when configuring Sawtooth JavaScript Transaction Processor on Ubuntu 16.04

Currently delving into the sawtooth example My progress so far : Downloaded and set up the latest version of Node (8.11.3) and npm. Commenced working on javascript essentials using docker-compose up. The hurdles I'm facing: I am looking to config ...

PHP function - retrieving the beginning element in the array that is returned for every row

My current database setup involves a MySQL DB with approximately 100 rows. Within each row, there is a list of items stored in a column within the table. The challenge I am facing is extracting the first item from this list in each row. For example, if the ...

Issue with jquery_ujs: Font Awesome spinning icon animation functions properly in Chrome but not in Safari

I've integrated font-awesome-rails into my Rails project. When a user clicks the submit button on a form: The submit button should display an animated font-awesome spinner and change text to "Submitting...". The button must be disabled to prevent m ...

Guide to sending and receiving JSON data using XAMPP PHP

Currently, my XAMPP 7.1.10-0 server is up and running with the following index.php file: <?php if(isset($_POST["username"])) { echo $_POST; header("Location:getbooks.php"); exit; } else { echo file_get_conten ...

Steps for showing an alert notification when the form field is empty

Within my script, there is a Bootstrap form where I am facing an issue. If the user fails to select a language and leaves the value blank or as "Select Language," I want to display an alert message prompting them to choose a language before proceeding to t ...

Using PHP to display a message box and redirecting to a different webpage

Currently, I am working on developing a login page using PHP. When the user enters an incorrect username/password combination, I display a JavaScript alert box to notify them. However, after clicking the "OK" button on the alert box, I want to redirect t ...

Does the input password typically submit on its own?

When attempting to create a form, I encountered an unexpected behavior. <form> <input type="text" autocomplete="username" style="display:none;"> <label for="password">password:</label><input type="password" autocomplete="c ...

Configuring the camera matrix manually within a ThreeJS environment

I am currently attempting to manually adjust the matrix of a camera in a basic three.js scene. Despite trying various methods such as using the matrix.set function combined with matrixAutoUpdate = false, the scene renders initially but does not update over ...

Transforming videos into a series of individual images

Recently, I created an HTML5 app that can record videos of about 20 seconds in length using a specific function. My goal is to convert these recorded videos into image sequences and then integrate them back into the app for additional processing. With a se ...