Is there a way to use the same color picker tool for adjusting both the background and text colors simultaneously?

My goal is to create functionality where the user can change the background color by clicking on the background of the div, and change text color by clicking on the text within the div.

 var box1 = document.querySelectorAll('.color1');
var picker = document.getElementById('colorpicker');
setInterval(() => {
       let color = picker.value;
          for (let i = 0; i < 2; i++) {
          box1[i].style.backgroundColor = color;
             }
                }, 200);
div{
  height: 200px;
  width: 20%;
  margin-top:10px;
}
<input id = "colorpicker" type = "color" value = "#ffffff">
 <div class = "color1" contenteditable = 'true'> color changes </div>
<div class= "color1"id = "c" contenteditable = 'true'> how are you </div>

Answer №1

To create a toggle effect, consider introducing a new variable such as isTextClicked. When isTextClicked is true, update the text color; when it's false, alter the div color instead.

This method might be useful, but it could be improved by having separate color pickers for each element. :)

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

Using Laravel and Vue to initialize authentication from the store prior to rendering any components

I'm currently immersed in a project that involves Laravel and Vue. My Objective I aim to authenticate a User before displaying any Vue Component to show the username in the Navbar. Challenge The issue I'm facing is that the Vue Navbar Component ...

Issues with Bootstrap's center-block functionality

I'm having trouble centering the navigation in my WordPress template that uses Bootstrap. I've tried using the center-block class on different elements, but the menu items are still aligning to the left. I even created a custom class in my custom ...

Ways to restart character count to zero if textarea is blank using jQuery

Can someone assist me with resetting the character count to zero when the textarea field is empty in jQuery? Currently, I have implemented code that successfully adds the character count as shown below; $(document).ready(function() { $('#content& ...

Attempting to preserve the CSS transform effect as I switch between menu options

Whenever I stop hovering over my element, it reverts back to its original position. However, I want it to remain in place when I am navigating through the "sousmenu" menu and only return to its original position when I am not hovering over the "sousmenu". ...

Using the map method in JavaScript, merge two separate arrays to create a new array

In my ReactJS project, I have created two arrays using map function. The first array is defined as follows: const hey = portfolioSectorsPie.map(sector => sector.subtotal); const hello = portfolioSectorsPie.map(sector => sector.percentage) The value ...

Navigating the division between presentational and container components in React/Redux

Here is a basic outline of my current setup: container.js: import Presentation from './Presentation'; import { connect } from 'react-redux'; export default connect( ({ someState}) => ({ ...someState }), (dispatch) => { ...

Removing user mentions in a message using Discord.js V12

I am working on a discord bot using discord.js and I am trying to create a custom *say command. The issue I am facing is that whenever I mention @everyone, or role IDs like <@&542542636743557>, the bot also pings them. Is there a way to replace ...

Obtain the template as a string within Vue

Let's examine the scenario of having a single file component in Vue with the following structure: // Article.vue <template> <div> <h1>{{title}}</h1> <p>{{body}}</p> </div> </template> If w ...

How to display a PDF in a web browser using PHP

I have the following code block: header('Content-type: application/pdf'); header('Content-Disposition: inline; filename="the.pdf"'); header('Content-Length: ' . filesize($file)); @readfile($file); It is working perfectly fin ...

Creating Table Rows on-the-fly

Seeking assistance and guidance from the community, I have modified code from an online tutorial to allow users to dynamically add rows to a table when data is entered in the row above. However, I am facing an issue where I can only insert one additional ...

Is there a way to retrieve and gather all data from various scopes and then access them collectively?

I attempted to scrape information from a website using Node.JS + Cheerio + Axios. I was able to retrieve all the necessary data, but encountered an issue with returning the data from different scopes in order to receive it. Currently, I can only obtain the ...

I need help figuring out how to send a POST/GET request from AJAX to a custom module controller in Odoo 10, but I'm running into issues

I have implemented a custom module in Odoo 10 with a simple controller. Everything works smoothly when accessing http://127.0.0.1:8069/cmodule/cmodule through the browser, displaying the expected return string. However, I encountered an issue when attempt ...

Utilizing props to transfer data between components in ReactJS

I have a React application where I am binding text values when a specific div is clicked. However, I am now faced with the challenge of passing these text values to another component using props. This is what my code looks like: class PostOptionBar exten ...

Dilemma arises from conflicting javascript codes

Currently, I am developing a web application where the main page features a timeline that needs to update its content automatically. To achieve this, I am utilizing the setTimeOut function of JQuery to refresh the timeline every x seconds. In addition, th ...

Is there a way to ensure that an AJAX request to a PHP file is synchronized with submitting form data to that same file?

My issue is that update.php is only able to retrieve the posted form data (post_edit). The variables sent earlier through AJAX are not being passed through. Notice: Undefined index: id_to_edit in ...\update.php on line 5 Notice: Undefined index: col ...

Creating a versatile design using a combination of grids, HTML tables, and media queries to ensure

When it comes to developing a responsive website, which method is superior: utilizing grid systems, employing media queries, or relying on HTML tables? ...

Styling Divs Beside Each Other Using CSS

I'm attempting to display the "left" and "right" divs side-by-side in the following example. However, they are not appearing next to each other as expected. Can someone point out my mistake? Thank you. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

Updating the query parameters/URL in Node.js's request module

In my Express.js application, I am utilizing the npm request module to interact with an internal API. The options passed to the request function are as follows: requestOptions = { url : http://whatever.com/locations/ method : "GET", json : {}, qs : { ...

Avoid triggering react-query to fetch data on page load, but instead set up a manual refetching mechanism

Currently, I'm utilizing react-query v3.13 for retrieving information from an API. Instead of fetching data immediately when calling useQuery, my goal is to fetch the API periodically starting from a specific point (such as clicking a start button). ...

Assigning a value to a variable can prevent the occurrence of an

My recursive code consists of two pieces aiming to print out half of the array recursively until we reach arrays of length 1. Surprisingly, the code without variable assignment runs infinitely, while the code with variable assignment behaves as expected. ...