Skip to content

Instantly share code, notes, and snippets.

@manifestuk
Created October 9, 2012 13:51
Show Gist options
  • Save manifestuk/3858951 to your computer and use it in GitHub Desktop.
Save manifestuk/3858951 to your computer and use it in GitHub Desktop.
DropDate: Display A List of Available Dates as Radio Buttons in a SafeCracker Form
<form id="custom_form">
<!-- The available date options. -->
<label><input name="dummy_date" type="radio" value="2012-10-05" /> 5th October, 2012</label>
<label><input name="dummy_date" type="radio" value="2012-10-06" /> 6th October, 2012</label>
<label><input name="dummy_date" type="radio" value="2012-10-07" /> 7th October, 2012</label>
<!-- The actual date fields (hidden). -->
<input id="real_date_fieldname_year" name="real_date_fieldname[]" type="hidden" value="null" />
<input id="real_date_fieldname_month" name="real_date_fieldname[]" type="hidden" value="null" />
<input id="real_date_fieldname_day" name="real_date_fieldname[]" type="hidden" value="null" />
<input type="submit" value="Let's Do This!" />
</form>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('#custom_form').submit(function() {
var $chosenDate, dateParts;
// Retrieve the selected date.
$chosenDate = $('input[name=dummy_date]:checked');
// If no date has been selected, our work here is done.
if ($chosenDate.length == 0) {
return;
}
// Populate the hidden fields.
dateParts = $chosenDate.val().split('-');
$('#real_date_fieldname_year').val(dateParts[0]);
$('#real_date_fieldname_month').val(dateParts[1]);
$('#real_date_fieldname_day').val(dateParts[2]);
});
});
})(jQuery);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment