Having difficulty selecting a nested ul with CSS, I'm currently exploring different options

For the website I am working on, I have a cms system that is responsible for rendering menus. My current task involves selecting the children of the parent menu items, and here is how the code is being generated:

<div class="menu">
    <ul>
         <li></li>
         <li><a href="" class="CurrentButton" ... />text</a>
              <div>
                   <ul></ul>
              </div>
         </li>
    </ul>
</div>

Despite my attempts with CSS to select it, I have not been successful except for using display: none;

.menu ul li div {
    display: none;
}

.menu > ul > li > a.CurrentButton div {
    display: block;
}

Could someone help identify what I might be doing wrong? Would it be easier to use a jQuery function for this task? I am new to jQuery, so I'm not sure how to approach writing a function for it.

The goal is to select the div within the li when the anchor inside that li has the class CurrentButton. If the anchor in the li does not have the class, then I want the div hidden.

Answer №1

The examples you provided both rely on locating the .menu element, which was not present in your code before. It is now included.

a.CurrentButton div will target any divs that are within a a.CurrentButton. However, your divs are not nested inside the a tags. You can try this instead:

.menu ul > li > a {
    //targets all the anchor tags, but none of the divs
}

.menu ul > li > * {
    //selects any elements within an 'li', including both 'a's and 'div's
}

If you want to select divs following a.CurrentButtons, you can use the following selector:

.menu ul li > a.CurrentButton + div {
    //targets any 'div's directly following 'a.CurrentButton'
}

Answer №2

For precise targeting, utilize the adjacent elements operator (+).

.menu > ul > li > a.CurrentButton + div {

Alternatively, you may be selecting a non-existent div that is a descendant of CurrentButton.

If specificity isn't crucial, stick with the same selector as before:

.menu > ul > li > div {

Answer №3

If the <ul> mentioned above is nested within an element with the class .menu, and the <div> in question is not a child of a.CurrentButton, you will need to target it using the following CSS selector:

.menu > ul > li > div {
    display: block;
}

Remember, the use of the > symbol signifies selection of only the direct children of an element.

Answer №4

Give this a shot:

Within your HTML, the div is located next to a.CurrentButton as a sibling. Therefore, you will need to use the + sign.

.menu ul li div {
    display: none;
}

.menu > ul > li > a.CurrentButton + div {
    display: block;
}

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

Error: ...[i] has not been defined

What seems to be the issue with this part of the script: function refreshLabels() { // loop through all document elements var allnodes = document.body.getElementsByTagName("*"); for (var i=0, max=allnodes.leng ...

The overflow-anchor property is not effective for scrolling horizontally

My goal is to create a never-ending horizontal scroll that allows users to scroll both left and right. When users scroll to the left, new content should be added to the beginning of the scrollable element (similar to scrolling through a schedule history). ...

Why isn't my custom HTML attribute displaying correctly?

In my current React app project, I have been incorporating custom attributes to HTML tags and React components for End-to-End (E2E) tests using Testcafe. However, I am facing an issue where the additional data-test="burger-menu-btn" attribute is ...

Unable to retrieve JSON using the getJson method

I've been transitioning to using jQuery exclusively, but I can't seem to get this function to work. My JSON file is pretty simple: { "status": "active" } All I'm trying to do is check if something has switched on or off. I created a basic ...

What causes the immediate firing of the DOM callback in Angular 1.2.9?

Check out a live demo here View the source code on GitHub here Angular 1.2.9 brought in DOM callbacks with the introduction of $animate:before and $animate:after events triggered during animations. However, it seems that the $animate:after event is trigg ...

Customizing SwiperJS to display portion of slides on both ends

I need some assistance with my SwiperJS implementation to replace existing sliders on my website. The goal is to have variable-width slides, showing a landscape slide in the center with a glimpse of the preceding and following slides on each side. If it&ap ...

modify a column in a database table when a different field is chosen

I am working on a basic table with 4 columns, two of which are dropdown menus with the classes "ddm1" and "ddm2". Here is what I am trying to achieve: Users should not be able to select from "ddm2" until they have selected an option from "ddm1". When ...

JQuery drag and drop functionality experiencing issues in Chrome Browser specifically when integrated into a Joomla Article

I recently embedded a jQuery drag and drop example into my Joomla article. Surprisingly, it works perfectly fine on Firefox browser but encounters issues on Chrome. Although the drag and drop functionality works on Chrome, the problem is that the buttons b ...

Issue with JSONP success function not being triggered

Here is an example of an Ajax call being made to a different domain: <script> $.ajax({ url: 'url?callback=?', dataType: 'jsonp', success: function (data) { alert(data[0].DeviceName) ...

Issues with CSS animations and transformations on SVG files are not being resolved

I've been attempting to create a transform animation using an SVG file, but unfortunately, the animation or transform isn't functioning at all. I've taken the time to research this issue, however, I haven't found a solution yet. Can any ...

What is causing the issue with using $("div[id*='\|']") in Internet Explorer 8 and 9?

section, have you encountered the issue where jQuery selectors fail to work with special characters escaped in IE8 and IE9? Presented below is the specific div that needs to be located: <div id="hello|12345"></div> The jQuery selector bein ...

Suspend Coontact-Host Upload of Documents

Is there a way to pause or resume uploading, or resume a broken upload using PHP, JavaScript, or jQuery without having to rely on Java, Flash, or C-type programming? Perhaps utilizing PHP socket functions? ...

Ways to serve JSON response following a 400 error code

After a user submits incorrect form details, such as an invalid username or blank email address, I make an Ajax request to my REST API. The response data I receive is structured as follows: HTTP 400 Bad Request Allow: POST, OPTIONS Content-Type: applicati ...

"Enhance Your Website with Creative Image Hover Effects using HTML

I'm looking to add a simple hover effect to the images AboutUsLink.png and AboutUsColourLink.png, but I'm unsure of the process. Here is the HTML code: <section class="link1"> <a href="#SecondLink"><img src="images/LogoAndLin ...

Navigate to a specific element using Selenium WebDriver in Java

Currently, I am utilizing Selenium along with Java and ChromeDriver to execute a few scripts on a website. My goal is to scroll the driver or the page to a specific element positioned on the webpage. It is important that this element is visible. I am awa ...

Amchart 5: Tracking Cursor Movement on the X Axis

I am a beginner with amCharts5 and I am in need of assistance to retrieve the actual X value of the cursor on my chart (where the X axis represents dates). I have come across helpful examples for amCharts 4, but nothing seems to work for amCharts 5. Is thi ...

Advanced AJAX image gallery with a touch of fancybox

I'm attempting to implement a fancybox image gallery using ajax for the first time. My knowledge of fancybox and jQuery is limited, but here's what I've managed to put together so far. Take a look at the gallery link below; <a class="fan ...

Unable to scroll following an ajax request

I am encountering an issue where I need to make an ajax call that populates a DIV and the content becomes as long as 2 web pages. However, I am unable to scroll unless I resize the window. How can I instruct the browser to recalculate the page content size ...

Having trouble properly wrapping divs using nextUntil() method

I am facing an issue with wrapping elements in HTML. The code snippet I have tried is not giving me the desired result. $('.cards .card-image').each(function() { $(this).nextUntil(".card-description").addBack().wrapAll("<div class='c ...

Sending data to a JSON file with jQuery and AJAX: Step-by-step guide

While there are numerous tutorials available on how to retrieve data from json files using jQuery and ajax, I have been unable to find any that demonstrate how to POST data to a json file. If anyone has a script or example they could share with me on how t ...