Dynamically populate the options of a select, from AJAX call, and add conditional the selected option?

Dynamically populate the options of a select, from AJAX call, and add conditional the selected option?

Hello,

I have an AJAX call created by changing the value of select element, and I get the response array with data to populate another select element. This itself is working for me now but I'm not convinced/satisfied how do this.

This how I have realize it now:
  1. if (result.data[i].fsel) {
  2.     jQuery('#'+flID)
  3.         .append(jQuery("<option></option>")
  4.         .attr("value", result.data[i].tlfc)
  5.         .attr("selected", "selected")
  6.         .text(result.data[i].tlfn);
  7. } else {
  8.     jQuery('#'+flID)
  9.         .append(jQuery("<option></option>")
  10.         .attr("value",result.data[i].tlfc)
  11.         .text(result.data[i].tlfn));
  12. }
But I was asking myself and trying to it on another way and at once like:
  1. jQuery('#'+flID)
  2.     .append(jQuery("<option></option>")
  3.     .attr("value", result.data[i].tlfc)
  4.      + if (result.data[i].fsel)
  5.            .attr("selected", "selected")  +
  6.     .text(result.data[i].tlfn));
so that I have not an if ... else, but the attribute "selected" conditional.

I've been googling but I couldn't find this specific example, but I have the feeling that such must be possible.

BR.,

Nico