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

123
Vistas
Select OnChange same value twice

I have a selectbox which adds articles to a list with jQuery like this:

$('#articles').on('change', function(e) {
    //adds article to list
}

This works fine, but when I try to add the same article twice, the onChange method does not fire since it is selected already.

What would be the nicest way to solve this problem?

I do not want to deselect it since it shows the user which item they selected before, and the onClick function is also not working well.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can achieve this by using a click event on the option tag.

$('#articles').on('click', 'option', function(e) {
    //adds article to list
    console.log($(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="articles">
<option>option 1</option>
<option>option 2</option>
<option>option 3</option>
<option>option 4</option>
<option>option 5</option>
</select>

The event will be called every time you click an option or change an option and then you can to add it to the array as before.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Using the select2 plugin it is possible to use:

  $('#articles').on('select2:close', function(e) {
      ...
  }

This will fire the function, even if it is selected already.

Thanks to: @Pete for the answer in the comments

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use select2()

$('#articles').select2().on('click', function() {
    console.log('selected : ', $(this).val());
});

$('#articles').select2().on('click', function() {
  console.log('selected : ', $(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<select id="articles">
  <option>1</option>
  <option>2</option>
  <option>3</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