Apply the child's style if the parent's sibling contains the specified class

I am struggling to add the class "bold" to a child element (span) of a parent element (button.usa-nav-link). This parent element has a sibling (ul li.active) with the class .active. When the sibling is active, I want the child element's text to be bold.

However, I am having difficulty targeting the parent element to access the child (span) element. I have tried using :has( > .active) and other methods without success. Any suggestions?

if(jQuery("button.usa-nav-link").siblings("ul").find(".active")){
     jQuery(this).children("span").addClass("bold");
};
.bold{
  font-weight:bold;
  font-size:22px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li><button aria-controls="submenu-771" aria-expanded="false" class="usa-accordion-button usa-nav-link"><span>Products</span></button>
<ul aria-hidden="true" class="usa-nav-submenu" id="submenu-771">
<li class="expanded menu-mlid-771">
<a href="/dev/products">Products</a>
</li>
<li class="first leaf menu-mlid-776">
<a href="/dev/content/imagery">Imagery</a>
</li>
<li class="last leaf menu-mlid-775">
<a href="/dev/content/web-data-services">Web Data Services</a>
</li>
</ul>
</li>
<li><button aria-controls="submenu-773" aria-expanded="false" class="usa-accordion-button usa-nav-link"><span>Services</span></button>
<ul aria-hidden="true" class="usa-nav-submenu" id="submenu-773">
<li class="expanded active-trail active menu-mlid-773">
<a class="active-trail active" href="/dev/page/services">Services</a>
</li>
<li class="first leaf active-trail active menu-mlid-956">
<a class="active-trail active" href="/dev/page/services#fireprogramsandtechnicalsupport">Fire Programs and Technical Support</a>
</li>
<li class="leaf active menu-mlid-953">
<a class="active" href="/dev/page/services#inventoryandmonitoring">Inventory and Monitoring</a>
</li>
<li class="leaf active menu-mlid-777">
<a class="active" href="/dev/page/services#mapanddatadevelopment">Map and Data Development</a>
</li>
<li class="last leaf active menu-mlid-955">
<a class="active" href="/dev/page/services#scienceandtechnologyevaluation">Science and Technology Evaluation</a>
</li>
</ul>
</li>

Answer №1

It seems like this is what you're searching for.

  • If you want to access the 'this' attribute while iterating through and checking each button with a specific class.

  • When utilizing the 'find' function, ensure to use the length attribute to verify whether it returns 0 or not based on the quantity of elements discovered for the value supplied inside the find function.

$("button.usa-nav-link").each(function(){

if ($(this).siblings("ul").find('.active').length){
       $(this).children("span").addClass("bold");
    } 
});
.bold{
  font-weight:bold;
  font-size:22px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li><button aria-controls="submenu-771" aria-expanded="false" class="usa-accordion-button usa-nav-link"><span>Products</span></button>
<ul aria-hidden="true" class="usa-nav-submenu" id="submenu-771">
<li class="expanded menu-mlid-771">
<a href="/dev/products">Products</a>
</li>
<li class="first leaf menu-mlid-776">
<a href="/dev/content/imagery">Imagery</a>
</li>
<li class="last leaf menu-mlid-775">
<a href="/dev/content/web-data-services">Web Data Services</a>
</li>
</ul>
</li>
<li><button aria-controls="submenu-773" aria-expanded="false" class="usa-accordion-button usa-nav-link"><span>Services</span></button>
<ul aria-hidden="true" class="usa-nav-submenu" id="submenu-773">
<li class="expanded active-trail active menu-mlid-773">
<a class="active-trail active" href="/dev/page/services">Services</a>
</li>
<li class="first leaf active-trail active menu-mlid-956">
<a class="active-trail active" href="/dev/page/services#fireprogramsandtechnicalsupport">Fire Programs and Technical Support</a>
</li>
<li class="leaf active menu-mlid-953">
<a class="active" href="/dev/page/services#inventoryandmonitoring">Inventory and Monitoring</a>
</li>
<li class="leaf active menu-mlid-777">
<a class="active" href="/dev/page/services#mapanddatadevelopment">Map and Data Development</a>
</li>
<li class="last leaf active menu-mlid-955">
<a class="active" href="/dev/page/services#scienceandtechnologyevaluation">Science and Technology Evaluation</a>
</li>
</ul>
</li>

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

Over-extended Affix in Bootstrap 3.1.0

I'm currently using Bootstrap 3.1.0. The issue I've encountered is that when the "affix" becomes too long for the viewport, it ends up getting cut off and doesn't display the bottom items. Is there a way to make Bootstrap's affix featu ...

Ways to resolve the issue with the information window on Google Maps

I have a problem with displaying infowindows in Google Maps; currently, it only shows the information for the last marker added. let myLatlng = new window.google.maps.LatLng(-33.890542, 151.274856 ); let mapOptions = { zoom: 13, cent ...

"Although all error messages are functional in Postman, they are not appearing correctly on the frontend client

I am currently developing a website using React and Node. While testing my register and login functionality in Postman, I encountered an issue where the error message for login (e.g., user doesn't exist, username or password incorrect) is not displayi ...

Is there an efficient method for transferring .env data to HTML without using templating when working with nodejs and expressjs?

How can I securely make an AJAX request in my html page to Node to retrieve process.env without using templating, considering the need for passwords and keys in the future? client-side // source.html $.get( "/env", function( data ) {console.log(data) ...

How should v-select, item-text, and item-value be correctly utilized?

When I make an API call, the response is returned. https://i.sstatic.net/AsFzu.png from another perspective. https://i.sstatic.net/T4vqa.png I store the result of the API call in a variable called result = [];. To create a dropdown list, I use v-select ...

Is it possible to craft poetic lines with indents in HTML using blockquotes, pre, or dd tags?

I am struggling to format a few lines of text with indentations using HTML. Here is an example I found: HERE Below is my attempt: <p><code>"Knowing is not enough. We</code></p> <p><blockquote><code>must apply. Wi ...

Restricting slash commands in Discord.js to a specific permission level

I'm currently developing a purge command, and I'm struggling to restrict its usage to users with the MANAGE_MESSAGES permission. Below is the source code for the client.on("ready") section as well as the entire command logic. Any assistance on ...

Introduce new material at the start of each line, akin to what ::before accomplishes for the initial line

I am currently working on customizing the appearance of my <code>/<pre> tags while ensuring that users can still easily highlight and copy the code. The method I had in mind (shown below) only applies to the first line and doesn't repeat ...

The jquery function will only run on the second click of the button, not the first one

I am struggling to create a JavaScript function that hides divs after clicking a button. The problem is, it only works after the second click, nothing happens on the first one. There are three buttons: #1, #2, and a button with the id #hide_ico. When I cl ...

Placing a background image behind the text of an <a> tag

I am wondering if it is possible to change the position of a background-image without affecting the text in a link. <a href="#" style="background-image: url('bg.png')">text</a> Can the background-image be moved independently of ...

LESS — transforming data URIs with a painting mixin

Trying to create a custom mixin for underlining text, similar to a polyfill for CSS3 text-decoration properties (line, style, color) that are not yet supported by browsers. The idea is to draw the proper line on a canvas, convert it to a data-uri, and the ...

Retrieving Progress Updates from a PHP Script Using XMLHttpRequest

I am currently using JavaScript to execute an XMLHttpRequest to a PHP script that returns data. My goal is to display a progress bar for the user instead of a spinning circle, indicating the progress of fetching and receiving the data. While I understand t ...

Discover Vuejs: Display Less, Reveal More

In order to achieve a specific task, I need to display 12 items in 4 columns with 4 items each. Initially, only the first four items are visible and the rest are hidden until the user clicks the "Show More" button. Upon clicking the button, the next row ...

Creating a polyBezier or polyCurve with Canvas HTML: a step-by-step guide

Looking to connect several points with a curve rather than just a straight line. I attempted using the lineTo() and bezierCurveTo() methods to draw the points. Is there anyone who can assist me in solving this dilemma? Perhaps there is a different approac ...

Show concealed elements above everything else

I've encountered an issue with my custom dropdown list as it displaces other elements when it appears. I want it to overlay on top of everything else, similar to the default select behavior. Despite trying to set position: relative; and z-index:100;, ...

Creating an animated time-based scrollable bar chart in javascript

As someone who is new to JavaScript and charting, I am seeking assistance with a specific task. Despite browsing various JavaScript charting libraries and examples, none seem to address my issue: My goal is to generate dynamic stacked bar charts, as depic ...

Creating a Circular Design with a Centered Line and Text Above and Below using Qualtrics (HTML/CSS/Java)

My knowledge of creating shapes and using these programming languages is limited. I am currently working on a survey in Qualtrics and I need to insert text into circular shapes. Creating one circle was straightforward, thanks to some helpful discussions ...

Aligning content within a div using Bootstrap 4

For a demonstration of this code, please visit the following Codepen link: codepen <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstr ...

Is there a way to submit a basic HTML form using R?

As a newcomer to R programming, I am excited to apply what I am learning in the Johns Hopkins Data Science track to practical use. My current goal is to automate the process of downloading historical bond prices from the US Treasury website After explorin ...

Border for status input like on Facebook

I tried to investigate with Firebug, but struggled to find a solution. How does Facebook wrap the border around the autosize input in the status section? I am particularly curious about the small triangle attached to the border. While using Firebug, I loca ...