Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
How to trigger a function before select dropdown

How to Trigger an function before drop down of select and abort dropdown if returned false?

maybe something like this:

document.querySelector('select').addEventListener('click',(e)=>{ 
 if(check()==false)
  e.target.abortdropdown();
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can put a container around the selectbox and bind it to the click event. then you check your check() function and can act on what happens to the select box. .

let check = () => {return false;};

const w = document.querySelector('#wrapper');
const s = w.querySelector('select');

w.addEventListener('click',(e) => {
  const os = s.querySelectorAll('option');
  
  if(check() == false){    
    s.setAttribute('onclick',"abortdropdown()");
    console.log('check() is false');  
    abortdropdown(os);
    
  } else {
    console.log('check() is true');  
    setdropdown(os);
    
  }      
});  


function abortdropdown(os) {
  os.forEach((el) => {
    el.classList.add('hide');
  })  
}

function setdropdown(os) {
  os.forEach((el) => {
      el.classList.remove('hide');
  })        
}
.hide {
  display:none;
}
<div id="wrapper">
  
  <select >
    <option class="hide">1</option>
    <option class="hide">2</option>
    <option class="hide">3</option>
  </select>
  
</div>

UPDATE

Here is a much better example. The wrapper / parent container (can also be your body) is triggered via mouseover in this example (load or similar would also fit). This checks your check() function. And would then set the selct box disabled or remove disabled. This should actually do what you want.

const parent = document.getElementById('parent');
const s = parent.querySelector('select');

let check = () => {return true;};

parent.addEventListener('mouseover', (e) => {
  
  if(check() == false){    
    s.setAttribute('disabled',"disabled");
    console.log('check() is false');      
    
  } else {
    console.log('check() is true');  
    s.removeAttribute('disabled')    
  }  
});
select[disabled] {pointer-events:none}
<div id="parent">  
  <select disabled>
    <option class="hide">1</option>
    <option class="hide">2</option>
    <option class="hide">3</option>
  </select>
  
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you are looking for this, https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault.

Then the if inner case should be.

If (check==false) {
    e.target.preventDefault();
}

And that stops further default handling of the event.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda