Is there a way to use jQuery to verify if an LI element contains a parent LI element?

Struggling with customizing the comments section of a WordPress theme? Utilizing jQuery for styling can be tricky, especially when dealing with nested UL elements like admin comments. The challenge lies in traversing the DOM structure to target specific elements for CSS adjustments.

Here's an attempt at solving this using jQuery:

$('.commentlist li.admin').each(function() {
  if ($(this).parents('li').size() > 0 ) {
    //Has parent LI, so this is a child comment
    $(this).children('.avatar').css({'background-position':'right -2530px'});
    $(this).children('.avatar img').css({'border-right':'1px solid #fff','border-bottom':'1px solid #fff'});
  }
  else {
    //Has no parent LI, top level comment
    $(this).children('.avatar').css({'background-position':'0 -2530px'});
    $(this).children('.avatar img').css({'border-right':'1px solid #fff','border-bottom':'1px solid #fff'});
  }
});

The goal is to style elements within "top level" LI elements with the class "admin" differently from those within "nested" LI elements. While the current approach involves checking for parent LIs, there seems to be a roadblock in achieving the desired outcome...

Any suggestions or alternative methods?

PS- Below is a snippet of the HTML layout:

            <ul class="commentlist">
            <li>
                ... (HTML structure example)
            </li>
            <li class="admin">
                ... (HTML structure example)
                <ul class="children">
                    <li>
                        ... (HTML structure example)
                    </li>
                    <li>
                        ... (HTML structure example)
                    </li>
                </ul>
            </li>
        </ul>

To better understand the issue and test possible solutions, check out the online demo:

Answer №1

It appears that all nested elements fall under the .children class, correct?

Perhaps a solution like this would work:

ul.commentlist > li.admin > .border-fake > .comment-body > .avatar {
    background-position: right -2530px;
}
ul.commentlist > li.admin > .border-fake > .comment-body > .avatar > img {
    border-right:1px solid #fff;
    border-bottom:1px solid #fff;
}

ul.children .avatar {
    background-position:0 -2530px;
}
ul.children .avatar > img {
    border-right:1px solid #fff;
    border-bottom:1px solid #fff;
}

UPDATE:

It seems that you have 4 unique selector pairs:

    <!-- Top level non-admin -->
ul.commentlist > li > .border-fake > .comment-body > .avatar
ul.commentlist > li > .border-fake > .comment-body > .avatar > img

    <!-- Top level admin ( should override non-admin ) -->
ul.commentlist > li.admin > .border-fake > .comment-body > .avatar
ul.commentlist > li.admin > .border-fake > .comment-body > .avatar > img

    <!-- Nested level non-admin -->
ul.children > li > .border-fake > .comment-body > .avatar
ul.children > li > .border-fake > .comment-body > .avatar > img

    <!-- Nested level admin ( should override non-admin ) -->
ul.children > li.admin > .border-fake > .comment-body > .avatar
ul.children > li.admin > .border-fake > .comment-body > .avatar > img

The selectors with the .admin class take precedence over those without.

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 button event is unresponsive within the Bootstrap modal

I've been attempting to capture the button click event within a modal, but unfortunately, it's not functioning as expected. Am I overlooking something crucial? Below is the header section of my HTML file, which is built using Django. {% load st ...

Bootstrap: when divs cover the jumbotron

I have recently started exploring bootstrap and have been using it to build a homepage for my website. However, I am facing an issue: The three images are overlapping the jumbotron section and I can't seem to figure out why. Below is the HTML code sn ...

Is it possible to load a jQuery Mobile page from a separate domain?

How can I load a jQuery AJAX Enabled page from an external source? I am developing an app using phonegap and one of the pages needs to be hosted online for real-time updates. However, I have been unable to figure out how to accomplish this so far. Simply ...

Supervising the organization of HTML sections for an offline web application

Currently, I am developing an offline HTML5 application that involves a significant amount of DOM manipulation through the creation of HTML strings within JavaScript functions. For example: var html=''; html+='<div class="main">&apos ...

"Responsive modal with dynamic height based on percentage and a minimum height constraint

My goal is to design a modal dialog that adjusts its height based on a percentage of the browser window height. The modal should maintain a minimum height to ensure it doesn't become too small, and it should dynamically grow, shrink, and re-center as ...

Extracting JSON information from the callback function

Can the msg variable in JavaScript be returned from the callback function? I attempted to do so, but received a null value, even though msg contained data within the scope of the callback function. var msg = load(); function load() { $.ajax({ ...

Reacting like sticky bottoms and tops

I'm working on a react/tailwind project that involves a component I want to be fixed at both the top and bottom of the screen. In simpler terms, there's an element that I need to always stay visible even when the user scrolls up or down the page ...

Proper alignment of div elements using Bootstrap

I am attempting to align 2 div elements side by side using Bootstrap code: <div class='col-12'> <div class='row'> <div class='col-6'> </div> <div class='col-6&a ...

Steps to remove a specific child element using jQuery:

CSS: <h1><br style="clear:both;"></h1> I am currently working on a project that involves using the Wikipedia API. One issue I've run into is cleaning up the raw HTML output from Wikipedia, specifically dealing with removing a speci ...

Tips for showcasing the individual product page in full width on woocommerce

Here is a link to the single product page: I am wondering if it's possible to extend the product summary drop-down menus all the way to the right without impacting the mobile site? Thank you. ...

What is the best way to preload a font with a hashed filename and style hosted on a CDN?

Is there a way to preload the Material Icons font in advance? <link rel="preload" href="https://fonts.gstatic.com/s/materialicons/v125/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2" as="font" type="font/woff2" crossorigin=&q ...

Conceal mobile button

Welcome to my website: Is there a way to hide the button on mobile phones? I want to create different buttons specifically for mobile devices. Below is the HTML code for the buttons. HTML <a href="http://site9171756.91.webydo.com/Info.html" target="p ...

The feature to swap out the thumb of a range slider with an image is currently not

I'm attempting to design a unique range slider using CSS. <style> .slide-container { width: 300px; } .slider { -webkit-appearance: none; width: 100%; height: 5px; border-radius: 5px; background: #FFFFFF; outline: none; opacity: ...

A data table created with Bootstrap is not displayed inline with the pagination feature

Customizing Bootstrap Data Table Code Instructions on how to customize this code: <div class="row"> <div class="col-sm-5"> <div class="dataTables_info" id="example_info" role="status" aria-live="polite"> Showing 1 to 10 of 2 ...

Can you use JQuery to limit the number of list items displayed to only four, removing any extras

How can I navigate from class="Mr Richard Has Lots of Trees" to class="MrContent", locate class="Stuff", and display only four li elements while removing the last one? I'm currently trying to grasp Jquery but struggling to understand it. <div id= ...

Is it possible to center the background button in bootstrap?

Having an issue with the alignment of the + sign on the bootstrap button, it's not centered as shown in this image : https://i.sstatic.net/lxacy.png. Attempted to enclose the button within a div using justify-content-center align-content-center text ...

The PHP response is constantly changing and adapting, creating

My webpage has a button that triggers an ajax request to a php page, all working well. I have a database called messages, with columns for "id", "receiver", "sender", and "message". Let's say there are two entries in the database where both the sender ...

Using jQuery UI autocomplete for replacing specific characters with regex

I have a search input that utilizes jQuery autocomplete to query a database. While it's currently functioning properly, I'd like it to be more flexible with certain characters. For example, if the data in the database is oc-5, I want the requeste ...

Issue with updating the content of a div using Ajax in combination with jQuery tablesorter

I've been encountering difficulties with my tablesorter and ajax div content update. Whenever the ajax reloads, all the tablesorter functionalities disappear. I attempted to use livequery, but it only works for the initial listing of the table. <s ...

Using PHP code in CSS for the background styling

I'm having trouble with this line of CSS... background-image:url(upload/<?php echo $array['image']; ?>); It's not working properly, and all I see is a blank screen. The image exists in the database and on the server in the corre ...