最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use HTML5 color picker with event listener - Stack Overflow

programmeradmin3浏览0评论

I am looking for a way to use eventlisteners and getting every color change by moving the mouse over the palette. It works only without eventlistener and I would like to know why. Where is my mistake...

It works, if I use the following code. Moving the mouse over the palette, fires every color change.

HTML:

<input type="color" id='picker_1' oninput="getData(this.value)">

Javascript:

function getData(value){console.log(value);

Using an eventlistener, I get only values by opening or closing the picker, but not with moving over the palette.

HTML:

<input type="color" id='picker_1'>

Javascript:

let el = document.getElementById('picker_1');
el.addEventListener('oninput', function() {  // I tried also onchange
 console.log(el.value);
});

The best solution would be to use the standard HTML5 color picker with jQuery...

Happy for some help. Thanks

I am looking for a way to use eventlisteners and getting every color change by moving the mouse over the palette. It works only without eventlistener and I would like to know why. Where is my mistake...

It works, if I use the following code. Moving the mouse over the palette, fires every color change.

HTML:

<input type="color" id='picker_1' oninput="getData(this.value)">

Javascript:

function getData(value){console.log(value);

Using an eventlistener, I get only values by opening or closing the picker, but not with moving over the palette.

HTML:

<input type="color" id='picker_1'>

Javascript:

let el = document.getElementById('picker_1');
el.addEventListener('oninput', function() {  // I tried also onchange
 console.log(el.value);
});

The best solution would be to use the standard HTML5 color picker with jQuery...

Happy for some help. Thanks

Share Improve this question asked Aug 10, 2021 at 16:40 HaraldHarald 2891 gold badge4 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

There is no event as oninput when using addEventListener, you should remove the on prefix and use input instead.

let el = document.getElementById('picker_1');
el.addEventListener('input', function() {  // I tried also onchange
 console.log(el.value);
});
<input type="color" id='picker_1'>

发布评论

评论列表(0)

  1. 暂无评论