Home > database >  how to trigger a function before select dropdown
how to trigger a function before select dropdown

Time:12-13

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();
})

CodePudding user response:

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.

CodePudding user response:

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) => {   
  if(check() == false){
    console.log('abortdropdown()');
    //e.target.abortdropdown();
  } else {
    console.log('check() is true');
  }      
});  
<div id="wrapper">
  
  <select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
  </select>
  
</div>

  • Related