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

219
Visualizações
JQuery generate drop-down list dynamic default selection

I am building a DataTable and everything renders perfectly fine.

one of the columns, I need to render a select/drop-down list and set default selected value.

I managed to render

render: function (data) {  
  //e.g. data.selectedvalue = 1 or 2 or 3
        
 var select = $("<select class='target' id='empstatus'><option value ='1'>Open</option><option value ='2'>Close</option><option value ='3'>N/A</option></select>");

 //tried to set value - but it is not setting default selection, always shows the first selection after it renders
 select.val(data.selectedvalue).attr('selected', 'selected');


 return select.prop("outerHTML");
}                              

This list renders, my question how can I set dynamic selected value?

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

0

You need to set the attribute of the appropriaet option, not the dropdown, so that it will be included in the outerHTML.

render: function (data) {  
  //e.g. data.selectedvalue = 1 or 2 or 3
        
  var select = $("<select class='target' id='empstatus'><option value ='1'>Open</option><option value ='2'>Close</option><option value ='3'>N/A</option></select>");
  select.find(`option[value=${data.selectedvalue}]`).attr('selected', 'selected');

 return select.prop("outerHTML");
}    
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