"What is the best way to apply a class to a parent element based on the content of

I am attempting to apply a class to an li element based on its content. Currently, my code is applying the class to both the parent and child li elements, but I only want it applied to the specific li with certain content. JS FIDDLE

<ul class="Team">
    <li>Manager
        <ul>
            <li>Team Leader</li>
            <li>Php Developer</li>
            <li>Graphic Designer</li>
            <li>Game Developer</li>
        </ul>
    </li>
    <li>
        Admin
        <ul>
            <li>Assistant Accountant</li>
        </ul>
    </li>
</ul>

I would like to add the class .trend to the li element that contains the text Game Developer.

$('.Team li:contains(Game Developer)').addClass('trend');

However, this code applies the class to both the parent and child li elements. Can anyone provide guidance on how to select the li based on its content? Your help would be greatly appreciated.

Answer №1

To tackle the li within a ul, try utilizing the :not() and :has() selector,

$('.Team li:contains(Game Developer):not(:has(ul))').addClass('trend');

Check out the DEMO here!

Answer №2

Consider assigning a class to <li> tags that have the assistant attribute.

You can use the following code snippet:

$('.Team li.assistant:contains("Game Developer")').addClass('trend');

Answer №3

Let's narrow it down.

$('.Team > li > ul > li:contains(Game Developer)').addClass('trend');

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

What are the steps to create a CSS grid like this?

Any suggestions on how to create a grid template similar to the image shown in this picture? I am looking to achieve this design without rounded borders and without any gaps between the elements. ...

html content area below the text

I'm currently working on developing a mobile webpage using jQuery Mobile. Encountering some peculiar behavior with text inside a div or table cell that has a background color. It seems to have some extra whitespace below the characters, making it app ...

Troubleshooting the unresponsive nature of the multi-level menu

I have incorporated a specific plugin into my application Check out my application here To access the menu, click on the third green button followed by any yellow button on the right side. However, the menu appears underneath another div rendering it unc ...

What is the best way to create a deletion alert using jQuery?

I need assistance adding an alert when the delete button is pressed in jQuery. Currently, even when I press cancel it still deletes. Can someone provide guidance on how to properly implement a delete alert in jQuery? Below is my current code snippet: if ...

Choose Your Preferences When the Page Loads

I have a dropdown menu where I can select multiple options at once without checkboxes. When the page loads, I want all of the options to be pre-selected by default. I am aware that I can use $(document).ready() to trigger actions after the page has load ...

I encountered a frustrating issue with saving user input data using localStorage

One of my goals is to store the current inputs even if the user refreshes or closes the browser. The issue I'm facing is that when I select Yes in the radio button, then refresh the page or close the browser and reopen it, the No button is checked wh ...

Manipulating links using JQuery: avoiding the binding of new links

I need to update a website with 50 pages that currently doesn't use ajax, making it cumbersome to make edits since changes have to be made on all 50 pages individually. After removing duplicate HTML from all 50 pages, I'm facing some issues: fu ...

The Bootstrap 5 Navbar dropdown is not as responsive as the dropdown showcased in the official documentation

I tried utilizing Bootstrap 5 to implement a dropdown functionality, but unfortunately, the dropdown on my website isn't behaving as expected. Unlike the dropdown in the official Bootstrap documentation, my dropdown remains open at all times even when ...

Tips on implementing SCSS styles in my create-react-app application along with Material UI components?

I recently developed a React app using Create-React-App, and now I am looking to incorporate Material UI with styling through SCSS. https://i.sstatic.net/JLEjX.png To manage my styles, I created a main.css file using node-sass. While attempting to apply ...

The influence of 'overflow' values, aside from 'visible', on the block formatting context

Not quite like this particular question on Stack Overflow, mine doesn't involve float issues but rather troubles with applying margins to a block-level element's only child. I encountered a dilemma while attempting to add a margin to an only-chi ...

Switching Present Tab in Rails

Within my application, I have a set of tabs displayed at the top thanks to a general layout in application.html.erb. Here's how they are structured: <li class="current"><%= link_to "Home", provider_path(current_user.id), :method=> "GET"%& ...

Obtaining the access token from the URL: A step-by-step guide

Once I have successfully authenticated with Google, the URL I receive is: http://localhost:3000/_oauth/google#access_token=ya29.5HxuYol1Io8JLeGePDznbfkkwu_PC4uodKwG8_1clFYAn9AgdOV1WGpOTNQP3s76HAsn7Y4zWw&token_type=Bearer&expires_in=3600 I am tryi ...

The layout of the keyboard in Cordova has been altered

Just starting out with my first Cordova app and I'm working on a simple login form. The issue I'm facing is that whenever I click on an input element, the keyboard pops up and completely messes up my layout, which should be straightforward. Has ...

What is the best way to use margin-top and margin-bottom in CSS?

Looking to perfectly center this modal in the middle of the screen with some margin all around. However, struggling to apply margin specifically to the top and bottom! export function Modal() { return ( <div className="fixed inset-0 z-[60] ...

Is it possible to embed a section of code from a different file?

Looking for a solution to streamline the process of updating multiple web pages with identical header and footer content. Currently have 5-10 pages with the same information, making it time-consuming to manually update each one when changes are needed. I ...

VueJs and the behavior of styling elements with inline CSS

Within my HTML, there is an <input> element that has the ability to change the background color of a <p> through an inline style. I conducted an experiment using two different methods to apply the style, and I encountered a peculiar behavior: w ...

Eliminating an SVG component from the webpage with the help of jQuery

Utilizing jQuery to insert an element into an embedded SVG can be achieved with the following code: var rect = SVG('rect'); $(rect).attr( { x: left, y: top, width: right - lef ...

Creating a jQuery alert similar to Stack Overflow's and integrating it with server-side functionality

I recently asked a question but unfortunately couldn't find the answer I was looking for. I came across this discussion on Stack Overflow about creating an alert box with dynamic data, but it wasn't exactly what I needed. After searching online ...

Transmitting a form containing an associative array through AJAX to PHP

Recently, I've been working on a form that has fields of an associative array: <label for="my_array[][system]">System</label> <input id="my_array[][system]" type="text" value="" name="my_array[0][system]"> <label for="my_array[] ...

Is it possible to dynamically load a PHP plugin in WordPress using jQuery and AJAX?

Is there a way to dynamically load a WordPress plugin shortcode using jQuery, Ajax, and PHP only when a specific button is clicked? Here's what I have attempted: 1) I've created an HTML snippet with a button and a DIV container: <a href="#" ...