Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Created September 12, 2012 22:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rmurphey/3710261 to your computer and use it in GitHub Desktop.
Save rmurphey/3710261 to your computer and use it in GitHub Desktop.
DRY Out Your Code with Functions
// repetitive code!
function updateDates() {
var startDate = $("#start-date").datepicker("getDate");
$("#menu-item-1 .day").text(startDate.getDate());
$("#menu-item-1 .month").text(Util.monthToText(startDate.getMonth()));
$("#menu-item-1 .year").text(startDate.getFullYear());
var endDate = $("#end-date").datepicker("getDate");
$("#menu-item-2 .day").text(endDate.getDate());
$("#menu-item-2 .month").text(Util.monthToText(endDate.getMonth()));
$("#menu-item-2 .year").text(endDate.getFullYear());
}
function updateDates() {
updateRegions('#start-date', '#menu-item-1');
updateRegions('#end-date', '#menu-item-2');
function updateRegions(datepickerElement, targetElement) {
var date = $(datepickerElement).datepicker('getDate');
var target = $(targetElement);
target.find(".day").text(date.getDate());
target.find(".month").text(Util.monthToText(date.getMonth()));
target.find(".year").text(date.getFullYear());
}
}
@wthit56
Copy link

wthit56 commented Nov 23, 2012

Just a little thing...

In the refactored code above, the updateRegions function will be created every time updateDates is called, won't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment