Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8051653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:20:52+00:00 2026-06-05T07:20:52+00:00

jquery dom jquery-selectors simple issue, but I cannot nail it. I am crating a

  • 0

jquery dom jquery-selectors simple issue, but I cannot nail it. I am crating a mega menu by manipulating a simple list element.

My menu structure looks something like this:

<ul>
    <li>
        <a href="#">Something</a>
        <ul>
            <li>
                <a href="#">Some Sub Menu Item</a>
            </li>
        </ul>
    </li>
</ul>

On hover I have a simple rule: Find all second level <li> elements and wrap them in a with class .subMenuContainer

Therefore, on hover, the above is transformed to this:

<ul>
    <li>
        <a href="#">Something</a>
        <ul>
            <div class="subMenuContainer">
                <li>
                    <a href="#">Some Sub Menu Item</a>
                </li>
            </div>
        </ul>
    </li>
</ul>

Simply put, sub menu items are wrapped in a div so they can be positioned specifically.

This works fine on the first hover, but on the second hover the subMenuContainer is wrapped yet again, so for ‘X’ amount of hovers I end up with ‘X’ amount of subMenuContainer elements.

My jQuery rule for wrapping these

  • elements is as follows:

    $('ul.someClass li').bind('mouseover', function(){
        $(this).find('ul').not(':contains(div.subMenuContainer)').find('li').wrapAll('<div class="subMenuContainer" />');
    });
    

    Surely by using the above code the items should only be wrapped in the div if there is not already a div with class .subMenuContainer inside the ?

    Am I missing something?

    Regards,
    Simon

    [EDIT] – See chosen answer below, the solution was simply to use > when searching for elements, eg: $('ul > li') which causes only <li> items that are direct children of the <ul> (and not in the div) to be found. Thank you.

    • 1 1 Answer
    • 0 Views
    • 0 Followers
    • 0
    Share
    • Facebook
    • Report

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    1 Answer

    • Voted
    • Oldest
    • Recent
    • Random
    1. Editorial Team
      Editorial Team
      2026-06-05T07:20:53+00:00Added an answer on June 5, 2026 at 7:20 am

      This happens because :contains is for usage with text, not elements.
      Try this:

      $(this).find('ul > li').wrapAll('<div class="subMenuContainer" />');
      

      or

      $(this).find('ul').children('li').wrapAll('<div class="subMenuContainer" />');
      

      Only li’s that are immediate childs of ul are wrapped now.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    A regular dom element is such as: document.getElementById('a') or document.createElement('div') But jQuery returns elements
    jQuery has wonderful selectors for DOM elements, but can you 'select' an event object.
    How to get all DEFINED CSS Selectors for a given DOM Element using jQuery?
    jQuery provides a browser neutral library for accessing and manipulating the DOM. This is
    Need some Advice on how to reference elements with jQuery (DOM/Traversing/Selectors). Sometimes its just
    Our application uses some jquery selectors to select some elements in the HTML DOM.
    I'm sure this will be a simple question but I still struggle with DOM
    I am developing a website which is extensively using jquery dom javascript ajax etc.
    How do I use jQuery to select DOM elements that do not have ID
    With jQuery you can bind functions to an event triggered on a DOM object

    Explore

    • Home
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help
    • SEARCH

    Footer

    © 2021 The Archive Base. All Rights Reserved
    With Love by The Archive Base

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.