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

104
Views
Selectpicker cancel change value event

Consider the following event:

$(`#selectpicker).on('change', function(){
if(condition) {} else 
{
  //canceel event change
}
});

How to cancel the change event based on specific condition therefore maintaining the same select picker value.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Is this what you are looking for? Set a variable for the current value and if an undesired value is selected we set the value of the select field back to the recent value.

<select id="selectpicker">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

let currentValue = $('#selectpicker').val();
$('#selectpicker').on('change', function(){
  let newValue = $(this).val();
  
  if(newValue == 3){
    console.log("3 is not allowed");
    $(this).val(currentValue);
    return;
  }
  currentValue = $('#selectpicker').val();
})

You could also disable certain options with "disabled" keyword, but keep in mind that this can be easily bypassed from developer tools.

<option disabled value="3">3</option>
about 4 years ago · Juan Pablo Isaza Report

0

While you can stop the execution of the callback with return false, the value will already have changed when this event is triggered. If you want to return to the previous value, you have to store it somehow, either in a variable or in a data property on the element. Then you can set the value back in your condition, or update the variable when your cancel-condition is not met.

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!