Looking for assistance with a CSS selector in jQuery

I am working with a WordPress blog that has multiple pages with dropdown subpages on hover. However, I only want the main pages to have links, not the subpages. To achieve this, I am using some basic JavaScript.

   jQuery(document).ready(function(){
   jQuery("#menus > li > a").attr("href","#");
 });

The current script selects every anchor tag, but I need it to target only the main pages. Here is the HTML structure for reference:

Here's an explanation:

The structure includes an unordered list (ul) with list items (li) containing anchor tags. If a list item also contains another unordered list (ul), those are subpages that appear on hover. Therefore, the anchor in the initial list item should have a href of "#", while other anchors should keep their existing href.

    <ul id="menus">
      <li>
        <a href="somelink">Main Page</a> // href should be changed to #
        <ul>
            <li>
              <a href="somelink2/">Subpage1</a>
            </li>
            <li>
              <a href="somelink3">Subpage2</a>
            </li>
        </ul>
      </li>
      <li>
        <a href="somelink">MainPage-with-no-subpages</a> // href should not be changed
      </li>
      <li>
        <a href="somelink4">MainPage</a> // href should be changed to #
        <ul>
          <li>
            <a href="somelink5">Subpage</a>
          </li>
          <li>
            <a href="somelink6">Subpage</a>
          </li>
        </ul>
      </li>
</ul>

Answer №1

attempt

jQuery(document).ready(function(){
   jQuery("#menus li > ul").siblings('a').attr("href","#");
});

Answer №2

Here is a solution that may meet your needs:

When the document is ready, this jQuery code will make it so that any anchor element within an unordered list item that contains a nested unordered list will have its href attribute set to "#":
jQuery(document).ready(function(){
   jQuery("#menus li:has(ul) > a").attr("href","#");
}

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 is the reason for the lack of an applied CSS selector?

.colored p{ color: red; } article > .colored{ color:powderblue; } .blue{ color: white; } <!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initi ...

Trouble Viewing Image File

I am facing an issue where I cannot upload an image file from my computer onto my HTML file, as it is not showing up in the browser. However, when I link an image file from the web, it works perfectly fine. I have double-checked the file path and ensured ...

convert webpages and images to PDF with a custom watermark

Looking to convert images fetched from the server into PDF format with a watermark by clicking a button. It would be ideal if there is a plugin that allows for PDF preview with disabled user selection and copy options. The website's platform is Sales ...

Modifying the color of the accordion icon in Bootstrap 5.3 by utilizing a root variable

Is it possible to change the icon color of Bootstrap 5.3's accordion component to match the color of the heading text? I have successfully set the heading text color using a CSS root variable var(--body-text). However, when trying to override the SVG ...

Efficient Localization for Rails JavaScript Validation

Where can I locate the language file for this text on a Ruby on Rails form? What is the best way to translate it? ...

Having trouble drawing over buttons in HTML5 Canvas?

window.addEventListener("load", () => { const canvas = document.querySelector("#canvas"); const ctx = canvas.getContext("2d"); const color = document.querySelector("#color"); const strokeWeight = document.querySelector("#strokeWeight"); ...

The vertical tab layout in HTML and CSS is experiencing an issue where the active tab is not being shown above the

Currently, I am developing a web page featuring vertical tabs where each tab corresponds to an image and some content. However, the problem lies in the fact that the active tab is not displaying above the image as expected. In my attempt to resolve this i ...

Issue with plugin conflicting with Vimeo's Froogaloop

I've integrated the Vimeowrap Playlist plugin to create a playlist of videos from a specific channel. Everything seems to be working smoothly in that aspect, but now I'm facing an issue with tracking video events such as play and pause. When atte ...

Exploring the Benefits of Using JSON in AJAX Requests

Can you help me determine the best way to extract data from a php page using ajax in the form of a json array? Here is an example code snippet: $.ajax({ type: "get", url: "assets/update_cart_user_fstore.php", data: up, cache: false, su ...

Should I be concerned about including a non-existent CSS class?

When it comes to using jQuery and finding the values of text fields, adding classes seems like a common practice. However, I am concerned about how this may impact page loading. Is it efficient for the browser to search for all these classes? In the past, ...

Extend and retract within a row of a table

I need help with modifying a dynamically generated table to meet specific requirements. The table currently looks like this. The task involves hiding all columns related to Category B, eliminating duplicate rows for Category A, and displaying entries from ...

Modifying static content within jQuery tabs

Encountering an issue with jQuery $('#tabs').tabs();. When checking out the example on JSFIDDLE, I noticed that the content containing an external php file is always displayed, even when switching to other tabs. <li class="files"> < ...

"Harnessing the power of jcarousel for dynamic data retrieval

Currently utilizing a jcarousel feature: $(document).ready(function() { $('#mycarousel').jcarousel({ vertical: true, auto: 3, wrap: 'circular' }) }) This is configured as circular, and I have the fo ...

Displaying divs depending on dropdown selection - Troubleshooting script issue

When I try to display certain divs based on dropdown selection using the script below, it works fine on a simple page. However, when I implement it on my current development page, it completely messes up the layout, turning everything black and adding stra ...

Is there a way for me to effectively utilize the data from a successful

This is a form validation checker. I'm looking to incorporate an ajax success message feature. After all validations are complete, I would like to display a success message such as "You have successfully signed up". jQuery(function ($) { $(' ...

Escaping from its container, text breaks free with the power of CSS

Experiencing difficulties with CSS flex layout that are proving challenging to resolve. HTML: <div class="image-view"> <div class="info"> <span class="label title">d37ci4x.jpg</span> <span class="label scale ...

Is there a way to select an element within a nested list item only when the preceding list item is clicked using Jquery/JavaScript?

I am working with an unordered list that looks like this: <ul> <li><a class="foo" id="one">some text</a></li> <li><a class="foo" id="two">some text</a></li> <li><a class="foo" id="thr ...

Internet Explorer is unable to recognize the .less file format

I have created a less file that works perfectly in Chrome and Firefox, but it is not being recognized in IE 9. Does anyone have suggestions on how to make it run in IE 9.0? Below is a sample of the CSS code: @import "../core/variables.less"; @import "Mi ...

Arrange text in a line below neatly in a list

My goal is to line up items on the same row, as shown in the screenshot. I need the opening hours to align consistently in the list. The data is retrieved from a database and displayed using Vue. This is how I currently display the opening hours and days. ...

One cool CSS trick: altering a div's background color with hover!

I'm working on creating a div that acts as a link to another page. My goal is to have the entire background color of the div change to blue when someone hovers over it. I want to achieve this effect using CSS only, as I am not sure if javascript will ...