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

218
Visualizações
how to obtain the drop down selected option from a dynamic table

I am trying to extract selected option from a dynamically created table column. The code below works to extract the input values but not for the drop down values.

Dynamically generate HTML

          $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='Letter"+i+"' type='text' placeholder='Letter' class='form-control input-md'  /> </td><td><input  name='Start"+i+"' type='text' placeholder='Start'  class='form-control input-md'></td><td><input  name='End"+i+"' type='text' placeholder='End'  class='form-control input-md'></td> <td>  <select name='cars' id='cars'><option value='All'>All</option><option value='Even'>Even</option><option value='Odd'>Odd</option></select></td>");

javascript code

    // start from the second row as the first one only contains the table's headers
    for (let i = 1; i < targetTableRows.length; i++) {
          var Inventorydict ={}

        // loop over the contents of each row
        for (let j = 0; j < targetTableRows[i].cells.length; j++) {
            // something we could use to identify a given item
            let currColumn = tableHeaders.cells[j].innerHTML;
            // the current <td> element
            let currData = targetTableRows[i].cells[j];
            // the input field in the row
            let currDataInput = currData.querySelector('input');
 
            // is the current <td> element containing an input field? print its value.
            // Otherwise, print whatever is insside
            currDataInput ? console.log(`${currColumn}: ${currDataInput.value}`) 
              : console.log(`${currColumn}: ${currData.document.getElementById("addressType")}`); 
            if (currDataInput) {

                Inventorydict[currColumn.replace(/\s/g, '')] = currDataInput.value;
  
            } else {
                Inventorydict[currColumn.replace(/\s/g, '')] = currData.innerHTML;
            }
             
        }

I use this to get the input values

    let currDataInput = currData.querySelector('input');

I tried using this to get the selected option

            let addresstype = currColumn.getElementById("addressType")

but it does not work. How can I obtain the drop down selected option ?

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

0

currColumn is being given a value in this point of your code:

let currColumn = tableHeaders.cells[j].innerHTML;

The innerHTML property returns a string, that's why you cannot use querySelector on it.

First of all, if you want to get the value of the select once every row, you should move that section outside of that inner cycle, that loops through each cell of that row.

Then, you will need to get a reference of the tr html element. This should be targetTableRows[i].

You can get your select element by using the querySelector on this element:

for (let i = 1; i < targetTableRows.length; i++) {
    var Inventorydict ={}
    
    // ...
    
    let select = targetTableRows[i].querySelector("select");
    let selectValue = select.value; // This will map to the "value" property of the selected option
}

However, please keep in mind that you should really select elements based on their class or input name, not by their type. Also, you should not assign the same id to multiple element of your page. This will help you avoid errors that will be hard to troubleshoot.

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