javascript - How to change background color using color picker without click on button? - Stack Overflow

I want to change the background color using colorpicker without a click on changecolor button. What I w

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>

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>

Share Improve this question asked Jun 21, 2021 at 4:54 Nitin SainiNitin Saini 5462 gold badges12 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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>

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)">

document.getElementById("colorpicker").addEventListener("change", function() {
  document.body.style.backgroundColor = this.value;
});
<input id = "colorpicker" type = "color">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744032053a4546740.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信