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

112
Views
How to Limit Year with Form Select Input Type?

I want to limit the year in the Select Input Type to only show 5 years from now and the year before

<div class="col-12">
     <select class="form-select bg-light border-0" style="height: 55px;">
             <option selected>Years</option>
             <option value="2018">2018</option>
             <option value="2019">2019</option>
             <option value="2020">2020</option>
             <option value="2021">2021</option>
             <option value="2022">2022</option>
      </select>
</div>

for example as above, but using dynamic code with PHP or javascript, please help me 😥

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

0

$(function () { 
 
  const currentYear = new Date().getFullYear();
  

  var oldYearValue=0, newYearValue=0; 
for (var i = 5; i >= 0; i--) {
   debugger
   oldYearValue= currentYear-i;
  
  if(oldYearValue != new Date().getFullYear()){
    
  
     $('#select1').append('<option value='+oldYearValue+'>'+oldYearValue+'</option>');
  }
  if (i==0){
    
    for (var j = 0; j <= 5; j++) {
   
   newYearValue= currentYear+j;
     $('#select1').append('<option value='+newYearValue+'>'+newYearValue+'</option>');
}
  }
}
   
 
 
  
  });
<script type="text/javascript" src="js/script.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<div class="col-12">
     <select class="form-select bg-light border-0" id="select1" style="height: 55px;">
             <option selected>Years</option>
       
      </select>
</div>

See Result Here

about 4 years ago · Juan Pablo Isaza Report

0

Might be this help for you in PHP

<div class="col-12">
 <select class="form-select bg-light border-0">
         <option disabled selected>Years</option>
         <?php
         for ($i = 4; $i >= 0; $i--) {
           $year_= date('Y', strtotime("-$i year"));
           echo '<option value="'.$year_.'">'.$year_.'</option>';
         }?>
  </select>
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!