Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!