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

158
Vistas
Sorting a html select box but ignoring the select

I've got a dyamically created html select box that i need to sort alphabetically but ignore the placeholder/option that says Select...

I've tired the following from here but realised the select box doens't have an ID only a Name

any ideas?

<select style="width:250px" name="sortbyco">
    <option value="">Select2</option>
    <!--I would like to keep this at the top-->
    <option value="40934">Africa (CAF)</option>
    <option value="44624">Asia (AFC)</option>
    <option value="29521">Europe (UEFA)</option>
    <option value="43099">North &amp; Central America (CONCACAF)</option>
    <option value="38731">South America (CONMEBOL)</option>
    <option value="46617">Oceania (OFC)</option>
    <option value="40934">Africa (CAF2)</option>
</select>



$("#sortbyco").append($("#sortbyco option:gt(0)").sort(function (a, b) {
    return a.text == b.text ? 0 : a.text < b.text ? -1 : 1;
}));
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Here's a snippet without JQuery. The selector now has an id for easier use in a css selector. A copy of the options array without the empty option is sorted, and re-added to the selector.

const selector = document.querySelector(`#sortbyco`);

// create an array of all options except the empty one
const opts = [...selector.querySelectorAll(`option:not([value='']`)];

// copy to a sorted array (note: use localeCompare to compare strings)
const sortedOpts = opts.sort( (a, b) => 
  a.textContent.localeCompare(b.textContent) );

// remove the old options from the selector
opts.forEach(opt => opt.remove());

// add the sorted options to the selector
sortedOpts.forEach( (opt, i) => selector.appendChild(opt) );
<select style="width:250px" id="sortbyco">
    <option value="46617">Oceania (OFC)</option>
    <option value="43099">North &amp; Central America (CONCACAF)</option>
    <option value="40934">Africa (CAF)</option>
    <option value="44624">Asia (AFC)</option>
    <option value="29521">Europe (UEFA)</option>
    <option value="40934">Africa (CAF2)</option>
    <option value="38731">South America (CONMEBOL)</option>
    <option value="" selected>Select</option>
    <!-- ▲ will be at the top after sorting -->
</select>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is a jQuery example.

$(function() {
  var opts = $("#sortbyco > option:not([value=''])").detach();
  opts.sort(function(a, b) {
    return $(a).text() == $(b).text() ? 0 : $(a).text() < $(b).text() ? -1 : 1;
  });
  $("#sortbyco").append(opts);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select style="width:250px" name="sortbyco" id="sortbyco">
  <option value="">Select2</option>
  <!--I would like to keep this at the top-->
  <option value="40934">Africa (CAF)</option>
  <option value="44624">Asia (AFC)</option>
  <option value="29521">Europe (UEFA)</option>
  <option value="43099">North &amp; Central America (CONCACAF)</option>
  <option value="38731">South America (CONMEBOL)</option>
  <option value="46617">Oceania (OFC)</option>
  <option value="40934">Africa (CAF2)</option>
</select>

This uses .sort() on the Array of jQuery Objects.

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