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

83
Vistas
populate select on page load

I've got it working for the first element, but as soon as i try to press another element it repopulates itself and selects the first element.

HTML:

<select id="line" name="line">
    <option selected="selected" disabled="true">Auswahl...</option>
</select>

Population:

$(document).ready(function(){
    var lineselects = document.getElementsByName("line");
    lineselects.forEach($line => $line.addEventListener('click', popLines));
    
});
function popLines(){
    fetch('ajax0.php',{method:'post'})
        .then( r=>r.json() )
        .then( json=>{
            line.innerHTML='';
            Object.keys( json ).forEach( key=>{
                let obj=json[ key ];
                line.append( new Option( obj.line, obj.line ) );
            })
        })
};

Necessary Information: I need to add the EventListener to every element of the NodeList as every Element is a select that needs to be populated. Should I split the populations up or is this fine too?

How do I prevent it from repopulating the select?

EDIT: For anyone stumbling on this answer, don't use the EventListener on every element of the NodeList, use the document.ready for every select you want to populate seperately -> if you press on any other element of the NodeList, only the first will get populated.

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

0

You are emptying the line <select> element empty with each fetch, but it looks like you want to empty it only for the first time, the best way I can think of is to use some sort of flag so that you know it's the first time.

$(document).ready(function(){
    var lineselects = document.getElementsByName("line");
    lineselects.forEach($line => $line.addEventListener('click', popLines));
    
});
function popLines(){
    fetch('ajax0.php',{method:'post'})
        .then( r=>r.json() )
        .then( json=>{

            if ( $( '#line' ).data('first-time') != 'no' ) {
                line.innerHTML='';
                $( '#line' ).data('first-time', 'no');
            }

            Object.keys( json ).forEach( key=>{
                let obj=json[ key ];
                line.append( new Option( obj.line, obj.line ) );
            })
        })
};
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