Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Create a 'select' drop-down menu in JavaScript

I'm having a problem in a simply js function. I have a web page where I show records in a table, and when I click on an icon (which stands for 'change this record'), that cell of table should be replaced with a dropdown menu in which the user can select the new value for that record based on the available ones.

I tried in a lot of ways:

function clickOnModifyProfessor(id, professors) {
    let select = document.createElement("select");
    select.setAttribute("id", id);
    for (let i = 0; i < professors.length; i++) {
        let el = document.createElement("option");
        el.textContent = professors[i]['name'] + " " + professors[i]['surname'];
        el.value = professors[i]['professorID'];
        select.appendChild(el);
    }
    //I set id=2 simply to test with one specific record, if it works I'll just set another parameter to generalize the change.
    const node = document.getElementById("2");
    node.textContent = '';
    node.appendChild(select);
}

With this code I just get the empty table cell after clicking on the modify button, but the strange thing is that if I try to create another element (textarea, password etc) it works perfectly, so I can't understand why it does this only with the select element. I also tried by creating the whole select with option elements and taking it to the table cell by the innerHTML, but it doesn't works (as before, it works with others elements that aren't 'select').

If it can help, here is the code fragment where I call this function:

echo "<tr><td>" . $row['subjectName'] . "</td>
         <td id='" . $rowsCount . "'>
            <p>" . $row['profName'] . " " . $row['profSurname'] . "
               <i class='material-icons' onclick='clickOnModifyProfessor(" . json_encode($row['subjectName'], JSON_HEX_APOS) . "," . json_encode($professors, JSON_HEX_APOS) . ")'>create</i>
            </p>     
         </td>
      </tr>";

PS: I also tried to print in console document.getElementById(id).innerText after I append the new 'select' element to the document and it prints correctly the options I added.

Am I making an error? I hope it's sufficiently clear.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I answer my own question with the solution I found, maybe it can be helpful for someone.

The problem is that the materialize css' select can't render on browser without starting js before, so it's necessary to add class='browser-default' when we create the select element or, if we want materialize css select style, we need to use $('select').formSelect(); after the select element has been created with its options.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda