Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save franszo/10f43126b04bc5aa4d1a13d5e1bb13cc to your computer and use it in GitHub Desktop.
Save franszo/10f43126b04bc5aa4d1a13d5e1bb13cc to your computer and use it in GitHub Desktop.
/* =FILTER BY DATE
----------------------------------------------------------------------------- */
jQuery.fn.filterByText = function(ary, selectSingleMatch) {
return this.each(function() {
var select = this;
var options = [];
$(select).find('option').each(function() {
options.push({ value: $(this).val(), text: $(this).text() });
});
$(select).data('options', options);
//empty select
var options = $(select).empty().scrollTop(0).data('options');
//iterate over days from array argument
$.each(ary, function(key, value) {
var search = value;
var regex = new RegExp(search, 'gi');
//if select contains upcoming days, then push into final array
$.each(options, function(i) {
var option = options[i];
if (option.text.match(regex) !== null) {
$(select).append(
$('<option>').text(option.text).val(option.value)
);
}
});
});
// if 1 result, make it default
if (selectSingleMatch === true &&
$(select).children().length === 1) {
$(select).children().get(0).selected = true;
}
});
};
function GetDates(startDate, daysToAdd) {
var aryDates = [];
var dd = '';
var mm = '';
for (var i = 0; i <= daysToAdd; i++) {
var currentDate = new Date();
currentDate.setDate(startDate.getDate() + i);
dd = currentDate.getDate();
if (dd < 10) {
dd = '0' + dd
}
mm = currentDate.getMonth() + 1;
if (mm < 10) {
mm = '0' + mm
}
aryDates.push(dd + "/" + mm + "/" + currentDate.getFullYear());
}
return aryDates;
}
/* =INIT
----------------------------------------------------------------------------- */
var now = new Date();
now.setDate(now.getDate() + 2);
var aryDates = GetDates(now, 5); //+1 (0)
$('#tfa_30').filterByText(aryDates, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment