Creating dynamic menu elements in HTML

How can I create movable menu items in HTML? Currently, I have four menu items arranged vertically in the right corner of my site like:

Home
Services
Contact 
About

I would like to make it so that when the user clicks on a menu item, it moves to the top with the other items shifting down accordingly. Any help or reference links would be greatly appreciated. Thank you!

Answer №1

One way to make the options jump straight to the top when clicked is by using this method:

$(function() {
  $('#menu').on('click', 'li', function(event) {
    $(event.target).prependTo('#menu');
  });
});
ul {
  padding: 0;
}
li {
  display: block;
  list-style-type: none;
  height: 30px;
  line-height: 30px;
  color: darkblue;
  font-family: sans-serif;
  background-color: #ddd;
  padding-left: 10px;
  margin: 5px 10px;
  cursor: pointer;
}
<script src= "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id= "menu">
  <li>Home</li>
  <li>Services</li>
  <li>Contact</li>
  <li>About</li>
</ul>

Click here for jsFiddle example

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

Wrap it in a ReactJS container tag

Today I encountered a frustrating issue while diving into ReactJS. I'm excited to learn by getting my hands dirty, but unfortunately, I keep running into this error: Adjacent JSX elements must be wrapped in an enclosing tag (47:14) And here's t ...

Accessing Google Analytics with a single OAuth token using the JavaScript API: A step-by-step guide

I'm currently developing a webpage for exclusive users to view shared Google Analytics data. I have managed to obtain an OAuth token for the account housing this data using JavaScript, but sadly it expires in just 1 hour. Is there a way to utilize th ...

Issues with collision detection between circles within grouped clusters

I am currently developing a game with circle-shaped gameobjects, similar to agar.io. I have implemented a collision system for these objects, which works well most of the time. However, when there are more than two objects in close proximity, the game beco ...

Div blur event for editing content

Utilizing a content editable div as an input control has presented some challenges for me. Unlike a textarea or input element, the div does not send information to the server automatically. To work around this issue, I have implemented a solution where I u ...

Verify that the input value changes onBlur

Is there a way to determine if the value of an input box has changed after blur? $("#username").on('blur', function() { $("#usertext").append("new input<br>"); }) Feel free to check out this jsFiddle example: https://jsfiddle.net/xztpts ...

Suggestions for integrating XMPP Chat into a webpage - keeping it light and efficient

What are your thoughts on incorporating a web-based "chat widget" on a website? Currently, I am using the following setup: OpenFire (latest Beta) Tigase Messenger XMPP library / webclient htttp://messenger.tigase.org/ The Tigase Messenger was customize ...

XSLT can be used to specify an HTML id attribute without surrounding quotes, like in the example `<div id=myId

In the HTML output file I am creating, I need to generate a div element with an id attribute. The value of the attribute should not be enclosed in quotes, similar to this example: <div id=myID>...</div>. However, everything works fine when us ...

Is it possible to use next.js to statically render dynamic pages without the data being available during the build process?

In the latest documentation for next.js, it states that dynamic routes can be managed by offering the route data to getStaticProps and getStaticPaths. Is there a way I can create dynamic routes without having to use getStaticProps() and getStaticPaths(), ...

Is it possible to dynamically adjust the container size based on its content with the help of *ngIf and additional directives?

I have a single-image container that I need to resize when editing the content. The size should adjust based on the incoming content. See the images of the containers below: Image 1: This is the container before clicking on the edit button. https://i.sst ...

Angular: Dynamically load Bootstrap modal with information from the selected item点击的元素加载数据

I am working with a list of items retrieved from a service, and I am using ng-repeat to display the data. What I want to achieve is the ability to open a bootstrap modal containing the specific data of the item that was clicked on. myApp.directive('o ...

Is there a method to prevent the repetition of this loop?

I have a question regarding my React app development. I am currently using useEffect to loop through six items every time, even when only one item has changed. How can I optimize this so that only the variable that was changed in the reducer function is ...

Utilizing Think ORM seamlessly across multiple files without the need to repeatedly establish a connection to the

I'm facing a situation where I have numerous models for thinky, and in each file I am required to create a new object for thinky and connect it multiple times due to the high number of models. var dbconfig = require('../config/config.js')[& ...

Iterating through an array with a .forEach loop and referencing the variable name

Currently, I am working on a project using JS/React and dealing with nested arrays in an array. The structure of my data looks like this: const ezra = ["Patriots", "Bears", "Vikings", "Titans", "Jets", "Bengals"] const adam = ["Chiefs", "Cowboys", "Packer ...

I'm looking for the location of Angular Materials' CSS directives. Where can I find them?

Currently, I am using components from https://material.angularjs.org/latest/ for a searcher project. One specific component I am working with is the md-datepicker, and I would like to implement some custom styles on it such as changing the background colo ...

Discovering how to navigate to a link within a web table using Cypress has been a challenge, as I keep receiving the error message stating that the element is not visible due to its CSS property being

Trying to click on the first enabled link in the 'Action' column of a web table has been proving difficult. In the example given, the first two rows do not have an enabled link, so the goal is to click on '8.5 AccountH' https://i.stack ...

Tips for displaying files from the public folder of express 4.0 as stylized HTML rather than plain files, depending on their extensions. For example, display JSON in a formatted way,

Whenever I place a file in the "public" directory of my express folder, it automatically generates a link on my webpage. For instance, by navigating to , I can download an image. Similarly, clicking on allows me to access a JSON file without any additiona ...

Building a Float32Array from an ArrayBuffer: A step-by-step guide

Let's say we have an ArrayBuffer containing vertices and normals, as displayed in the screenshot below: https://i.sstatic.net/LyNfW.png //recompute object from vertices and normals const verticesBuffer:ArrayBuffer = e.data.verticesBufferArray; const ...

Utilizing JQuery Ajax to Make Post Requests in Node.js and Express

I have been attempting to send an AJAX post request using JQuery to my Node.JS server running Express, but for some reason it is not functioning as expected! var express = require('express') var app = express(); app.listen(8888); app.configure( ...

gRaphael - the struggle of animating a line chart

Encountering an issue with the gRaphael javascript line chart library. Creating a line chart from a CSV file with five columns (# of minutes, time, waiting time, in treatment, closed, in location). Previously drew full chart without animation successfull ...

position an element at a higher level than the #top-layer

I developed a custom tool and utilized z-index to position it above all other elements: .customTool { position: fixed; z-index: 99999; } However, this functionality stops working when a modal <dialog /> element is opened in modal mode. As a res ...