Sure, here is the JavaScript code to change any div color when double click on any element:
Code snippet
// 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
<!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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is the JavaScript code to change any div color when double click on any element:
Code snippet
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
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.