Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

113
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda