• Hi, I’m working with javascript on a new custom color picker for a new plugin I’m working on.
    It works perfectly except for a problem. On the color input, I set two Event Listeners. The first on “change” (in a different plugin) and the second on “click” (in this new plugin).
    When with the new plugin I select a new color the input change and the new color is displayed; but the function that should be executed on “change” (in the other plugin) doesn’t fire.

    Does anyone as any idea why?

    here below is the code:

    html:

    
    <input id="color1" type="color" value="#ffffff">
    

    JavaScript Plugin one:

     
     const clr1 = document.querySelector('#color1');
    
     if(clr1){
       clr1.addEventListener('change', function(){
         // do something
       } );
     }
    

    I want to add the the first plugin work perfectly with the standard browser color picker

    JavaScript Plugin Two:

     
     const clr2 = document.querySelector('#color1');
    
     if(clr2){
       clr2.addEventListener('click', function(){
         e.preventDefault();
         this.disabled = true;
         opnePicker(); // it open the picker popup win
       } );
    
       // when I set the color in the input field
    
       var newclr = getColor() // it returns the clicked color
       clr2.disabled = false;
       clr2.value = newclr;
       closePicker(); // it close the picker popup win
     }
    

    The input color change it’s value, but as I said it doesn’t trig the first “change” event.

    • This topic was modified 4 years, 9 months ago by antonop4u.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Dion

    (@diondesigns)

    I believe the problem is your setting this.disabled = true, which if I’m not mistaken, will cause browsers to not trigger the onchange event. If that is the case, then using a custom event will solve the problem. Documentation:

    https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events

    Thread Starter antonop4u

    (@antonop4u)

    Hi Dion, thanks for your kind replay. I just checked, I commented the line this.disabled = true in all the function involved, and the problem still persists. Do you know how I can check if the “onchange” event is still there or if somehow it has been canceled by the new picker? BTW I’m going to test the custom event too. Thanks again.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘JavaScript event “change” doesn’t trig’ is closed to new replies.