1

I am trying to remove the DIV after nav and keep the menu simple like nav > ul > li

I tried unwrap but it doesn't work.

Here is the HTML structure

<nav>
  <div class="menu-headermenu-container">
    <ul id="menu-headermenu-1" class="menu">
      <li><a href="corporate-training/">Menu Item</a></li>
    </ul>
  </div>
</nav>
4
  • Is this page public so we can poke at it? Not many details here. Commented Dec 24, 2017 at 9:09
  • actually this is page not public, working within local Commented Dec 24, 2017 at 9:22
  • @sorak the code that you need to poke at is in the question. Commented Dec 24, 2017 at 9:26
  • I thought perhaps it was requesting modifications that would not break a functional dropdown menu, which would require examining the associated JS Commented Dec 24, 2017 at 9:29

6 Answers 6

4

You basically need to replace the div with its inner content

$(".menu-headermenu-container").replaceWith($("#menu-headermenu-1"));
Sign up to request clarification or add additional context in comments.

Comments

0

Use .contents() and .unwrap() as:

$("div.menu-headermenu-container").contents().unwrap();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div class="menu-headermenu-container">
    <ul id="menu-headermenu-1" class="menu">
      <li><a href="corporate-training/">Menu Item</a></li>
    </ul>
  </div>
</nav>

Comments

0

The .unwrap() method removes the element's parent.

$('#menu-headermenu-1').unwrap();

Plnkr example unwrap() demo

2 Comments

Did you apply .unwrap() method to the children, here the #menu-headermenu-1 to remove the div
In the plnkr example I provided inspect Menu Item and see that there is no div in the element hierarchy. Code in script.js
0

to remove the div WITHOUT the content: https://api.jquery.com/unwrap/

 $(".menu-headermenu-container").contents().unwrap();

to remove the div WITH the content: https://api.jquery.com/remove/

 $(".menu-headermenu-container").remove();

1 Comment

this will remove the nav, and OP clearly says they want to remove only the div.
0

Just use unwrap() to ul#menu-headermenu-1.

unwrap() remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.

$('ul#menu-headermenu-1').unwrap();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
  <div class="menu-headermenu-container">
    <ul id="menu-headermenu-1" class="menu">
      <li><a href="corporate-training/">Menu Item</a></li>
    </ul>
  </div>
</nav>

Comments

0

$(document).ready(function(){
$("nav").find(".menu-headermenu-container").contents().unwrap();
})
.menu-headermenu-container{
background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<nav>
  <div class="menu-headermenu-container">
    <ul id="menu-headermenu-1" class="menu">
      <li><a href="corporate-training/">Menu Item</a></li>
    </ul>
  </div>
</nav>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.