Ways to append each list item from one unordered list to the end of another based on their unique styles

I am in the process of making a website responsive and I am faced with the task of combining two different menus. In order to achieve this, I need to transfer all list items (li) from one unordered list (ul) to another.

Provided below is a simplified version of the current markup:

<ul class="navigation-menu">
  <li>...</li>
  <li>...</li>
  <li>...</li>
</ul>

<ul class="nav-bar">
  <li>...</li>
  <li>...</li>
  <li>...</li>
</ul>

The goal is to append all list items from nav-bar at the end of the navigation-menu list and then hide the nav-bar from being visible on the web page.

This is my attempted solution:

$('ul.nav-bar li').each(function() {
   $(this).after('ul.nav-bar:last-child');
}

I am uncertain about how to go about solving this issue. Any guidance or pointers in the right direction would be greatly appreciated.

Answer №1

If you want to add items to the bottom of the .navigation-menu, you can use the appendTo() method:

$('ul.nav-bar li').each(function() {
  $(this).appendTo('ul.navigation-menu');
});

To hide another div at a specific screen width, you can utilize media queries in CSS (although it will be hidden because it will be empty):

@media only screen and (max-width: 500px //example){
  .nav-bar{
    display: none;
  }
}

You could also achieve this with JavaScript if desired, but CSS would suffice:

$('ul.nav-bar').hide();

FIDDLE

Update

Thanks to vladkras for pointing out that the JavaScript code can be shortened to:

$('ul.nav-bar li').appendTo('ul.navigation-menu');

NEW FIDDLE

Answer №2

const mainNav = $('.main-navigation'),
    links = mainNav.find('a');
$('.menu').append(links);
mainNav.hide(); // or use .remove() to completely remove it from the DOM

JSFIDDLE LINK

Answer №3

Hello, I have an example for you to check out: Example

If you want to add more menu items after the existing second menu, all you need to do is clone the list elements and append them to the navigation menu using jQuery.

$(function(){
  var children = $(".nav-bar li").clone();
    $(".navigation-menu").append(children);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="navigation-menu">
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>

<ul class="nav-bar">
  <li>4</li>
  <li>5</li>
  <li>6</li>
</ul>

You can also hide elements based on a specific breakpoint, such as hiding the .nav-bar when the screen width is 769px or less.

@media screen and (max-width:769px){
    .nav-bar{
        display: none;
    }
}

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

Limit a div to only accept a specific stylesheet

Currently, I am immersed in a project that involves HTML5, MVC 4, CSS 3, JQuery, and LINQ. We have meticulously designed styles for various ui, li, and other html controls within the project. However, I now face the challenge of integrating a JQ Grid (htt ...

angularjs dynamically display expression based on controller value

I'm not the best at explaining things, so I hope you all can understand my needs and provide some assistance here. Below is a view using ng-repeat: <div ng-repeat="item in allitems"> {{displaydata}} </div> In my controller, I have the f ...

Tips for incorporating a User ID object into an Ajax request

Currently, I am delving into the world of web development and tackling a client-server project that involves allowing users to authenticate themselves and store their blood pressure readings. While the authentication part is functioning properly, I am enco ...

Attempting to change the text of a Bootstrap button using jQuery when clicked

Looking to create a mobile navigation menu that toggles between displaying "Menu" and "X" when clicked. Check out the code on jsfiddle $(document).ready(function() { $('.navbar-toggle').on('click', function () { if (matchMe ...

How come the last word in CSS nested flexbox wraps even though there is space available?

I am facing an issue with a nested flex container setup. Below is the code snippet: <div class="parent-container"> <div class="child-container"> <span class="color-block"></span> <span>T ...

Adjust the DIV dimensions to fit the background image perfectly

My question involves a DIV element with a specific style applied to it. .vplayer-container .logo { position: absolute; bottom: 50px; right: 10px; background: url(../img/logo.png) no-repeat; border: 1px solid #000000; max-width: 50px; max-height: 50 ...

Enhance the current MultiSelect object by incorporating JQuery MultiSelect

Is there a way to detect changes on a JQuery MultiSelect in order to trigger an update elsewhere? The typical javascript onchange method does not seem to work because the select itself does not change. However, I came across a method called "beforeclose" t ...

Maximizing the potential of AFRAME animations: Tips for recycling your animations

Looking to create a unique and irregular animation, similar to the pattern of a waterdrop falling: Drip nothing Drip Drip Drip nothing nothing Is there a method to achieve this effect or loop an extended animation sequence of dripping? ...

Issues with data within a Vue.js pagination component for Bootstrap tables

Currently, my table is failing to display the data retrieved from a rest api (itunes), and it also lacks pagination support. When checking the console, I'm encountering the following error: <table result="[object Object],[object Object],[object Ob ...

Detection of numerous Google Analytics tags

To troubleshoot a client's Google Analytics account connected to their website, I decided to utilize the Tag assistant by Google extension. Upon running it, an alert popped up displaying "Multiple Google Analytics tags detected." One tag was the one I ...

Exploring ways to access elements within shadow-root (open) in Angular using SVG.js

I'm currently tackling a project involving Angular Elements. Within this specialized component, my goal is to incorporate SVG.js 3+. However, due to the necessity of utilizing ViewEncapsulation.ShadowDom in my component, I am encountering challenges w ...

Making a column in a Vue data grid return as a clickable button

My goal is to utilize vue.js grid to display multiple columns with calculated text values, along with a clickable column at the end that triggers a dynamic action based on a parameter (such as calling an API in Laravel). However, when I include the last c ...

How to style HTML li elements to wrap like sentences within a paragraph

I am looking to format text list elements in a way that they wrap like sentences within a paragraph. Here is the accompanying HTML & CSS code snippet that demonstrates how li element behaves when it exceeds the width of the ul container. ul { ...

How to properly handle Angular routing errors and best practices?

Currently, I have been delving into learning Angular to integrate with my Ruby on Rails application. However, I have encountered some challenges specifically related to routing. Here is a snippet from my app.routing file: import { NgModule } from '@ ...

Can we set a specific length for an array passed in as a prop?

Can we use Typescript to specify the exact length of an array coming from props? Consider the following array of objects: const sampleArray = [ { key: '1', label: 'Label 1', value: 9 }, { key: '2', label: 'Label 2&ap ...

When utilizing JavaScript within an Electron application file that is linked with a script tag in an HTML document, there may be limitations on using node modules with

While working on my Electron application, I encountered an issue with referencing a node module using require within an HTML form that includes JavaScript. Specifically, I am having trouble integrating the 'knex' node module into my code. Here i ...

What is the purpose of uploading the TypeScript declaration file to DefinitelyTyped for a JavaScript library?

After releasing two JavaScript libraries on npm, users have requested TypeScript type definitions for both. Despite not using TypeScript myself and having no plans to rewrite the libraries in TypeScript, I am interested in adding the type definition files ...

Having trouble with the dropdown multiselect feature in AngularJS?

I'm striving to develop a straightforward multi-select dropdown utilizing angular JS, bootstrap, and JS. This dropdown should display days (mon, tue...sun), with options for select all and unselect all. My goal is to create a controller that will de- ...

Reactjs is retrieving several items with just one click on individual items

I am having trouble getting only a single sub-category to display. Currently, when I click on a single category, all related sub-categories are showing up. For example, in the screenshot provided, under the Electronic category, there are two subcategories: ...

Having issues with the functionality of the Previous/Next button in my table

I am facing a challenge with my table as I am trying to include previous/next button for users to navigate through it. However, the interaction doesn't seem to be functioning properly, and I suspect that I need to establish a connection between the bu ...