$(document).ready(function() { function initSelect2() { $("[id^='DDAlertFreq']").select2({ minimumResultsForSearch: Infinity, allowClear: false, theme: "bootstrap" }); }; //define the dropdown and set to variable var DDAlertFrequency = '' function RetrieveUserAlerts(uid) { $.ajax({ url: 'SavedSearches.asmx/LoadUserAlerts', dataType: 'json', method: 'post', data: { userid: uid }, success: function (data) { var tbl = $("#tblAlerts > tbody"); tbl.empty(); $.each(data, function () { userAlert.alert_idx = this['alert_idx']; userAlert.Name = this['Name']; userAlert.price_alert_low = this['price_alert_low']; userAlert.alert_frequency = this['alert_frequency']; userAlert.alert_max_per_day = this['alert_max_per_day']; userAlert.alert_to_email = this['alert_to_email']; userAlert.alert_to_sms = this['alert_to_sms']; userAlert.active = this['active']; userAlert.alert_start_date = moment(this['alert_start_date']).format("MM/DD/YY"); userAlert.alert_end_date = moment(this['alert_end_date']).format("MM/DD/YY"); userAlert.OpenSectionID = this['OpenSectionID']; // modify the dropdown to assign unique id and match selection var selectAlert = DDAlertFrequency.replace("DDAlertFreq", "DDAlertFreq_" + userAlert.alert_idx).replace('"' + userAlert.alert_frequency + '"', '"' + userAlert.alert_frequency + '" selected'); var tblRow = '
' + userAlert.Name + '
' + '' + '
' + '' + '
' + '' + '
' + selectAlert //modified Select2 dropdown + '
' + '' + '
' + userAlert.alert_start_date + '
' + userAlert.alert_end_date + '
' + '' + '
' tbl.append(tblRow); initSelect2(); //call the function to initialize all Select2 dropdowns created }); }, error: function (err) { console.log('Error (RetrieveUserAlerts): ' + JSON.stringify(err, null, 2)); } }); };