Menu background changes color while hovering over a new item being added

Currently working on a straightforward menu that allows users to add new items. The issue arises when the user clicks on the last div ("new item"), causing a new item to be created and shifting the last item to the right, resulting in a flickering hover effect on the last item.

// locating elements
var button = $("button")
var container = $('.container');
var newBtn = $('.new');

newBtn.on('click', function() {
$('<div class="row">blinking new</div>').insertBefore(newBtn)
})
body {
  margin: 0;
  padding: 0;
}
.container {
  display: flex;
  background: gray;
  height: 30px;
}
.row {
  background: green;;
  width: 100px;
}
.row:hover {
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">firstg </div>
  <div class="row">second </div>
  <div class="new row">new item</div>
</div>

Chrome: The background of the latest item blinks when the hover target changes.
IE11: When a new item is added, IE does not repaint the menu, resulting in the preservation of the hover state of the latest item even if the cursor is placed on the newly added item.
https://i.sstatic.net/6eQJu.gif Is there any workaround available to resolve this issue?

Answer №1

If you want to keep the hover state on the current element without having to switch, try changing the button into a new element and then creating a new button:

// locate elements
var button = $("button")
var container = $('.container');
var newBtn = $('.new');
function handleClick() {
    var newDiv = newBtn.clone();
    newDiv.insertAfter(newBtn);
    newBtn.html('blinking new');
    newBtn.off('click')
    newBtn = newDiv;
    newBtn.on('click', handleClick);
}
newBtn.on('click', handleClick)
body {
  margin: 0;
  padding: 0;
}
.container {
  display: flex;
  background: gray;
  height: 30px;
}
.row {
  background: green; 
  width: 100px;
}
.row:hover {
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">firstg </div>
  <div class="row">second </div>
  <div class="new row">new item</div>
</div>

Answer №2

The issue arises from how the new button is being inserted into the menu. It causes the "new item" to be moved to the end, while still retaining its hover state. As your mouse hovers over the new button, it gains focus and changes color to yellow.

To resolve this, use .append() to add the new button at the end of the menu. This way, the mouse remains over the same button throughout, preventing any change in focusing. Then, move the "new item" to the end using another .append().

In essence, the problem lies in the combination of your :focus CSS selector and the method of adding content with .insertBefore. Switching to .append() will eliminate this issue.

// locate elements
var button = $("button")
var container = $('.container');
var newBtn = $('.new');

newBtn.on('click', function() {
  // Add the new button at the end of the menu
  $(".container").append('<div class="row">blinking new</div>');
  
  // Move the "new item" button to the end of the menu
  $(".container").append(this);
});
body {
  margin: 0;
  padding: 0;
}
.container {
  display: flex;
  background: gray;
  height: 30px;
}
.row {
  background: green; 
  width: 100px;
}
.row:hover {
  background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">firstg </div>
  <div class="row">second </div>
  <div class="new row">new item</div>
</div>

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

Adding PHP information into a div using AJAX

Hello, I am currently working on an app using HTML5 and Javascript. I am looking to extract some data from it and display all the information in a div element. My knowledge of AJAX is limited as I am fairly new to it. Can someone guide me on how to retriev ...

Arranging nested columns in a different order for smaller devices with Bootstrap 4

I am currently in the process of developing an e-commerce section, which includes an aside containing filters and a "main" section that should have 6 items displayed as individual cards. The challenge arises when viewing this on mobile screens, where the a ...

Slide the menu off to the right

Check out these images to see my progress: Main Image. When you click on the menu, everything shifts right. I've managed to set up the push menu, toggle switch menu, and main divs. Now I need help with centering the 3 lines that are clicked within th ...

Can we make one tab disappear when another tab is clicked in Ionic 2 app menu icon?

I am working on a project using Ionic 2 and I have implemented multiple tabs in the application. However, I need to find a way to hide the top tab when the user clicks on the bottom tabs menu icon. Here is my Plunker for your reference. My goal is to ma ...

Align the <div> vertically within the <td> tag

My current set up looks something like this: <tr> <td> <div class="blue">...</div> <div class="yellow">...</div> </td> <td> <div class="blue">...</div> <div class="yellow ...

transfer a product attribute value to a different product attribute within the Magento platform

There is an attribute called Weight : Attribute Code: weight Scope: general Catalog Input Type for Store Owner: Text Field Values Required: yes Apply To: Simple Product, Bundle Product Allow HTML Tags on Frontend: yes Also, there is a General Weight attr ...

Adding a <script> tag to a div id using reactjs

I'm having trouble adding a tag into my React code. The componentDidMount function always runs before render. How can I add the script and ensure it runs together? componentDidMount () { function loadScript() { var script= document.creat ...

The forceShutdown function in the node.js grpc server does not properly terminate the server

I encountered an issue with restarting a grpc node js server after shutting it down When attempting to restart the grpc server after forceShutdown, I received the following error: Error: Server is already running This snippet shows the problematic code: ...

Replicate the action of highlighting a section of a website and copying it to the clipboard using JavaScript

I am currently in the process of transferring an outdated application to a new platform, but I'm facing difficulty understanding the changed JavaScript code. My goal is to find a parent HTML element named "output" and then select all its child elemen ...

Mysterious HTML/CSS element causing page to stretch beyond limits

I am currently updating a website to make it responsive. Check it out here: When the viewport is below 980px in width, a horizontal scroll bar appears and the html/body/container remains at 980px. I've attempted to apply an inline width of 200px to ...

Interactive features causing compatibility problems with Next.js 14 server communication, including API calls and data transmission to components

Currently, I am working with the architecture of the Next.js App Router and facing a challenge with my post page. This page fetches data from an API and passes it to a component. So far, I've managed simple interactions like style changes upon clickin ...

Creating an Angular directive that shares a single scope while displaying multiple behaviors

I am facing a specific scenario with my directive where the refresh scope needs to be either an object or a function. How can I properly assign this to my directive? 1. Initial scenario <!--HTML--> <directive refresh="vm.refresh"> </dir ...

Trouble accessing state when React child calls parent method

Within my project, I am working with 3 components that are nested as follows: App->GameList->GameItem The App (parent) component has a method that is triggered by the onClick event within the GameItem (child) component Upon clicking the GameItem co ...

Using AngularJS to prevent HTML injection in input fields

Is there an effective method to prevent HTML injection in input fields? As an example, if I have a search input field: <input id="search" type="text" ng-model="search" placeholder="search..."> I want to ensure that any attempts to input malicious c ...

Assign the default value of a Vue prop to the options of its parent component

I have a child component that needs to accept a value given in the component's parent's $options object as a possible default value. In the background, the component should be able to receive its data through a prop or from a configuration. Howe ...

Steps for creating a table with a filter similar to the one shown in the image below

https://i.sstatic.net/zR2UU.png I am unsure how to create two sub-blocks within the Business A Chaud column and Potential Business Column. Thank you! I managed to create a table with input, but I'm struggling to replicate the PUSH & CtoC Column for ...

What is the best way to showcase the contents of an array of objects from JavaScript on an HTML page in a dynamic

Looking for guidance in Javascript as a beginner. For an assignment, I need to create two separate JS files and use them to display data dynamically on an HTML page. One JS file, named data.js, contains an array of objects representing a product catalog. T ...

What is the best way to implement auto-refreshing with reactQuery?

Hey there! I'm currently working with a GraphQL API and I'm trying to automatically refetch data at regular intervals (e.g. every 3 seconds). I've been using React Query and have tried some workarounds like using setInterval, but it's n ...

Locating HTML snippets within a document

I am trying to extract all HTML <p>...</p> elements from a document. I have been attempting to use regular expressions (Regex) with the following pattern: Regex regex = new Regex(@"\<p\>([^\>]*)\</p\>" ...

Adding dots between page numbers when using ReactJS/Javascript Pagination can be achieved by implementing a specific method

I have created a pagination component using React JS. Currently, it only displays all the page numbers, but I want it to show dots when there are more than 8 total pages. Here's how I envision it: c. When total is less than 9 pages: Page 1 selecte ...