Trigger Select Event

Como lanzar un evento despues de otro evento

by Porfirio Chavez

HTML

<select id="uno">
  <option value="A">A</option>
  <option value="B">B</option>
</select>

<select id="dos" disabled>
  <option value="AB">AB</option>
  <option value="BB">BB</option>
</select>

JavaScript

const select1 = document.getElementById('uno');
  const select2 = document.getElementById('dos');

  const initialEvent = document.createEvent('HTMLEvents');
  initialEvent.initEvent('change', true, false);

  select1.addEventListener('change', function() {
    console.log('CHANGE en 1')
    const secondEvent = document.createEvent('HTMLEvents');
    secondEvent.initEvent('change', true, false);

    setTimeout(function() {
      select2.dispatchEvent(secondEvent);
    }, 2000);
  });

  select2.addEventListener('change', function() {
    console.log('CHANGE en 2')
    select2.removeAttribute('disabled');
  })


  // COMENTAR PARA HACERLO MANUAL
  select1.dispatchEvent(initialEvent);