Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background color using color picker without click on button?

I want to change the background color using colorpicker without a click on changecolor button. What I want is to change the background color as I am selecting RGB in colorpicker.

function changecolor(){
  let color = document.getElementById('colorpicker').value;
  document.body.style.backgroundColor = color;
  
}
<input id = "colorpicker" type = "color">
  <button onclick = "changecolor();"> change Color </button>
like image 596
Nitin Saini Avatar asked Oct 15 '25 18:10

Nitin Saini


2 Answers

Use this.

let colorpicker = document.getElementById('colorpicker');



  setInterval(()=>{
      let color = colorpicker.value;
      document.body.style.backgroundColor = color;
  }, 200);
  <input id = "colorpicker" type = "color" value="#ffffff">
    <button onclick = "changecolor();"> change Color </button>
like image 112
PRAFULL PATEL Avatar answered Oct 18 '25 11:10

PRAFULL PATEL


You can add onchange event on your colorpicker so that your function will get called and then simply change your background color .

Demo Code :

function changecolor(el) {
  document.body.style.backgroundColor = el.value;
}
<input id="colorpicker" type="color" onchange="changecolor(this)">
like image 37
Swati Avatar answered Oct 18 '25 10:10

Swati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!