admin管理员组文章数量:1356898
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
3 Answers
Reset to default 3Use 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">
本文标签: javascriptHow to change background color using color picker without click on buttonStack Overflow
版权声明:本文标题:javascript - How to change background color using color picker without click on button? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744032052a2579090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论