---
title: "Change any div color when double click on any element"  
description: "Change any div color when double click on any element"  
author: "Ethan Karla"  
published: 2021-07-29  
updated: 2023-06-14  
canonical: https://www.mindstick.com/forum/156669/change-any-div-color-when-double-click-on-any-element  
category: "jquery"  
tags: ["jquery"]  
reading_time: 2 minutes  

---

# Change any div color when double click on any element

How to change any div [color](https://www.mindstick.com/articles/77/how-to-split-form-background-color-in-c-sharp) when [double click](https://www.mindstick.com/forum/155692/what-is-dfp-double-click-for-publisher) on any element?\

## Replies

### Reply by Aryan Kumar

Sure, here is the JavaScript code to change any div color when double [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) on any element:

Code snippet

```plaintext
// Get all div elements
var divs = document.querySelectorAll("div");

// Add a listener for the dblclick event
for (var i = 0; i < divs.length; i++) {
  divs[i].addEventListener("dblclick", function() {
    // Get the current background color
    var currentColor = this.style.backgroundColor;

    // Change the background color to red
    this.style.backgroundColor = "red";

    // Set a timeout to change the background color back to the original color
    setTimeout(function() {
      this.style.backgroundColor = currentColor;
    }, 500);
  });
}
```

This code will add a listener for the dblclick event to all div elements. When an element is double clicked, the code will get the current background color, change the background color to red, and then set a timeout to change the background color back to the original color after 500 milliseconds.

Here is an example of how the code can be used:

Code snippet

```plaintext
<!DOCTYPE html>
<html>
<head>
<title>Change div color on double click</title>
</head>
<body>
<div>This is a div</div>
<div>This is another div</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
// Get all div elements
var divs = document.querySelectorAll("div");

// Add a listener for the dblclick event
for (var i = 0; i < divs.length; i++) {
  divs[i].addEventListener("dblclick", function() {
    // Get the current background color
    var currentColor = this.style.backgroundColor;

    // Change the background color to red
    this.style.backgroundColor = "red";

    // Set a timeout to change the background color back to the original color
    setTimeout(function() {
      this.style.backgroundColor = currentColor;
    }, 500);
  });
}
</script>
</body>
</html>
```

When you double click on any of the div elements, the background color will change to red. After 500 milliseconds, the background color will change back to the original color.


---

Original Source: https://www.mindstick.com/forum/156669/change-any-div-color-when-double-click-on-any-element

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
