Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!