Removing an element with a specific class that was created with Javascript can be done by clicking outside the box

I needed to eliminate an element that was created using JavaScript of a specific class when clicked outside the box.
I have created a color-picker using .createElement and added a class to it.
Now, my goal is to remove that picker whenever a click occurs outside its parent element because sometimes users do not select a color and on every click, a new color-picker is generated.

Here's how my function operates:

  • Clicking on the circle option will allow you to change either the borderColor or backgroundColor
  • When an option is selected, the color of that property is altered accordingly

var restyleBG = document.getElementsByClassName("restyleBackground");
restyleBG[0].addEventListener('click', changeBGcolor);

function changeBGcolor() {
  let optionChooseBackground = document.getElementById("optionToChooseBackground");
  optionChooseBackground.style.display = "block";
  let optionChooseTag = optionChooseBackground.getElementsByTagName("p")

  for (let j = 0; j < optionChooseTag.length; j++) {
    optionChooseTag[j].onclick = function() {
      var varWantToChange = optionChooseTag[j].innerHTML;

      let optionToChoosePicker = document.getElementById("optionToChoose");
      let colourPicker = document.createElement("input");
      colourPicker.type = "color";
      colourPicker.className = "colour-picker";
      optionToChoosePicker.appendChild(colourPicker);
      colourPicker.click();
      colourPicker.addEventListener('input', function() {
        var colorPickerVal = colourPicker.value;
        if (varWantToChange == "borderColor") {
          restyleBG[0].style.borderColor = colorPickerVal;
        } else if (varWantToChange == "backgroundColor") {
          restyleBG[0].style.backgroundColor = colorPickerVal;
        }
      })

    }
  }
}
#optionToChoose {
  border: 2px solid red;
  width: 200px;
}

#optionToChooseBackground {
  position: absolute;
  height: 100vh;
  width: 100vw;
  background-color: rgba(165, 42, 42, 0.205);
  display: none;
}

#clockOuterCircle {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 42vw;
  height: 42vw;
  margin: auto;
  border-radius: 50%;
  border: 4px solid rgb(255, 62, 62);
  background-color: rgb(253, 133, 133);
  user-select: none;
}
<div id="optionToChooseBackground">
  <div id="optionToChoose">
    <h3>Choose a style want to change :</h3>
    <h4>Border</h4>
    <p>borderColor</p>
    <p>backgroundColor</p>
  </div>
</div>

<div id="clockOuterCircle" class="restyleBackground"></div>

So far, everything functions as intended, but there's a problem with creating pickers on each click, regardless of whether a color is chosen or not (I did not use an onchange event on the color picker which would execute only when the color value is changed, resulting in removal of the picker. However, this approach is not practical when the user simply clicks on the property without changing the color).

The method I attempted is depicted in the code snippet below where clicking outside the color-picker's parent element should remove the created element, but unfortunately it throws an error when clicked.

var restyleBG = document.getElementsByClassName("restyleBackground");
restyleBG[0].addEventListener('click', changeBGcolor);

function changeBGcolor() {
  let optionChooseBackground = document.getElementById("optionToChooseBackground");
  optionChooseBackground.style.display = "block";
  let optionChooseTag = optionChooseBackground.getElementsByTagName("p")

  for (let j = 0; j < optionChooseTag.length; j++) {
    optionChooseTag[j].onclick = function() {
      var varWantToChange = optionChooseTag[j].innerHTML;

      let optionToChoosePicker = document.getElementById("optionToChoose");
      let colourPicker = document.createElement("input");
      colourPicker.type = "color";
      colourPicker.className = "colour-picker";
      optionToChoosePicker.appendChild(colourPicker);
      colourPicker.click();
      colourPicker.addEventListener('input', function() {
        var colorPickerVal = colourPicker.value;
        if (varWantToChange == "borderColor") {
          restyleBG[0].style.borderColor = colorPickerVal;
        } else if (varWantToChange == "backgroundColor") {
          restyleBG[0].style.backgroundColor = colorPickerVal;
        }
      })

      optionChooseBackground.addEventListener('click', optionChooseBackgroundClose)

      function optionChooseBackgroundClose() {
        if (event.target == optionChooseBackground) {
          let optionToChoosePicker = document.getElementById("optionToChoose");
          optionToChoosePicker.removeChild(colourPicker);
        }
      }
    }
  }
}
#optionToChoose {
  border: 2px solid red;
  width: 200px;
}

#optionToChooseBackground {
  position: absolute;
  height: 100vh;
  width: 100vw;
  background-color: rgba(165, 42, 42, 0.205);
  display: none;
}

#clockOuterCircle {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 42vw;
  height: 42vw;
  margin: auto;
  border-radius: 50%;
  border: 4px solid rgb(255, 62, 62);
  background-color: rgb(253, 133, 133);
  user-select: none;
}
<div id="optionToChooseBackground">
  <div id="optionToChoose">
    <h3>Choose a style want to change :</h3>
    <h4>Border</h4>
    <p>borderColor</p>
    <p>backgroundColor</p>
  </div>
</div>

<div id="clockOuterCircle" class="restyleBackground"></div>

Thank you very much in advance

If anyone has a better method to remove the picker than the one mentioned above, please feel free to share!

Answer №1

Here's a suggestion.

Note: I've made some changes to the code, but the HTML structure remains the same as yours.

var restyleBG = document.getElementsByClassName("restyleBackground");

restyleBG[0].addEventListener('click', changeBGcolor);

function changeBGcolor() {
  let optionChooseBackground = document.getElementById("optionToChooseBackground");
  optionChooseBackground.style.display = "block";

  
  let optionToChoosePicker = document.getElementById("optionToChoose");
  let colourPicker = document.createElement("input");
  colourPicker.type = "color";
  colourPicker.className = "colour-picker";
  optionToChoosePicker.appendChild(colourPicker);
  

 let optionChooseTag = optionChooseBackground.getElementsByTagName("p")

var varWantToChange = "";
  for (let j = 0; j < optionChooseTag.length; j++) {
    optionChooseTag[j].addEventListener("click", () => {
    varWantToChange = optionChooseTag[j].innerHTML;
    });

}
  
      
     
      colourPicker.addEventListener('input', function() {
        var colorPickerVal = colourPicker.value;
        if (varWantToChange === "borderColor") {
          restyleBG[0].style.borderColor = colorPickerVal;
        } else if (varWantToChange === "backgroundColor") {
          restyleBG[0].style.backgroundColor = colorPickerVal;
        }
      })
}
#optionToChoose {
  border: 2px solid red;
  width: 200px;
}

#optionToChooseBackground {
  position: absolute;
  height: 100vh;
  width: 100vw;
  background-color: rgba(165, 42, 42, 0.205);
  display: none;
}

#clockOuterCircle {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 42vw;
  height: 42vw;
  margin: auto;
  border-radius: 50%;
  border: 4px solid rgb(255, 62, 62);
  background-color: rgb(253, 133, 133);
  user-select: none;
}
<div id="optionToChooseBackground">
  <div id="optionToChoose">
    <h3>Pick a style you want to change:</h3>
    <h4>Border</h4>
    <p>borderColor</p>
    <p>backgroundColor</p>
  </div>
</div>

<div id="clockOuterCircle" class="restyleBackground"></div>

Answer №2

Ensure to generate a transparent overlay when launching the datepicker, covering the entire screen except for the selector area. Implement a closing event on this overlay.

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

Efficient Error Handling in Next.JS with Apollo GraphQL Client

Although the component successfully renders the error state, an uncaught exception is displayed in the console and a dialogue box appears in the browser. How can expected errors be handled to prevent this behavior? import { useMutation, gql } from "@a ...

Learn how to smoothly transfer a URL from JavaScript to HTML and initiate the download process

I created a website that solely relies on HTML5, CSS and JavaScript to function. The core functionality of this website involves utilizing YouTube URLs. I have stored the URL of a specific YouTube video as a variable, now I am looking for a way to transfer ...

Conceal the div once it reaches the top of the webpage

Is there a way to hide an "h1" element when its parent div reaches the top of the page and then show it again when it moves down using both JQuery and CSS? I attempted to utilize a scroll listener in JQuery to toggle a class, but this resulted in the "h1" ...

JavaScript Routing with Multiple Files

I tried to access api.js from Routes.js, but I encountered an issue stating that the function my_function_in_api is not defined. Here is my code, could you please help me identify where the problem lies: Routes.js var val = require('file name') ...

Wrapping dynamic page titles with comment tags in NextJS for cleaner code

I am currently working on NextJS version 12. In my project, I have a file named Layout.js which contains the following code: import Head from "next/head"; const Layout = ({children, pageTitle, mainClass}) => { return ( <> ...

The onChange function in React JS for a Material UI 'number' TextField retains the previous value

In this element, I have an onChange function: <TextField id="rowinput" type="number" defaultValue={this.defaultRows} // defaultRows = 1 inputProps={{ min: "1", max:"5"}} onChange= ...

Identifying CMYK Images Through Javascript Feature Detection

Many are aware that IE8 does not support CMYK JPG images, as it fails to render them at all. This post discusses the issue further: , among others. My inquiry is as follows: Is there a method to detect from JavaScript, akin to Modernizr, whether a browse ...

Setting a default value in react-select

Recently, I developed a react-select component that is compatible with redux-form. The SelectInput component looks like this: const MySelect = props => ( <Select {...props} value={props.input.value} onChange={value => props.input.on ...

Issue with a "hover" effect on a specific input[type="submit"] button

I'm having trouble getting my CSS styles to apply correctly to the input field. Can anyone help me understand why this is happening? If you'd like to take a look at the code, here are the links to the .html file and the .CSS styles. The goal is ...

Problem with exporting data from the API to a JavaScript file in Excel format

Instead of receiving actual data in the response, I am getting a set of characters. However, everything works fine when I click on Download file in swagger. Can someone help me diagnose the issue? function downloadDocFile(data: Blob, ext = 'xlsx' ...

A JavaScript async function that can be activated either by clicking the search button or by hitting the "enter" key in the input field

Recently, I've been working on an async function in Vanilla Javascript that is triggered by a click event on the search button. However, I would like to modify it so that the function can also be initiated when the "enter" key on the keyboard is press ...

Troubleshooting problem in AngularJS involving ng-repeat and storing user input

I am currently working on developing a system similar to Facebook's post/like/comment feature. I have successfully implemented the posts and likes functionality, but I am facing some challenges with comments. The issue lies in the controller code and ...

Choose all stylus/css imports except for those with a specific ending

How can a rule be correctly written in a .styl file to import all files from selected folders except those that contain the -ie suffix (e.g. bar-ie.style)? What is the best way to achieve this? The code below is not functioning as expected: import([&apos ...

Ways to prevent a div containing a lengthy content string from extending beyond other divs

Check out my Codepen project here Here is the HTML code for my personal website: <div id="splash" class="divider"> <h1 class="text-center text-uppercase">vincent/rodomista</h1> <p class="text-center text uppercase">Full Stack ...

Method for transmitting JSON array from Controller to View using CodeIgniter

I have a function in my controller: function retrieveAllExpenses() { $date=$this->frenchToEnglish_date($this->input->post('date')); $id_user=$this->session->userdata('id_user'); $where=array('date&ap ...

Maya model being loaded using Three.js

I've been following a tutorial on how to load Maya models using Three.js, which you can find here. Everything is going smoothly, but the tutorial only covers loading models with a single texture. Below is the source code snippet from the tutorial: ...

CSS - spacing anomaly among inline-block items

I am facing a strange issue with the arrangement of elements in my container div. I have set them to display: inline-block, and they are supposed to expand horizontally if the contents exceed the device width. However, there is an unexpected margin between ...

What's preventing me from aligning this div in the center?

I'm trying to set up a layout with two centered columns for Tumblr posts on the left side, while keeping a sidebar on the right. Can someone help me achieve this? #wrapper /*applying styles for two columns*/ { display: inline-bloc ...

Sentry platform is failing to record network-related problems

Incorporating Sentry into my Next.JS application has allowed me to easily detect JavaScript errors such as reference or syntax issues on the Sentry platform. Unfortunately, I have encountered some challenges as Sentry is not logging any network-related er ...

Tips for adjusting the width of the scrollbar track (the line beneath the scrollbar)

How can I style a scrollbar to match this specific design? https://i.stack.imgur.com/KTUbL.png The desired design specifies that the width of -webkit-scrollbar-thumb should be 5px and the width of -webkit-scrollbar-track should be 2px. However, it is pro ...