Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save faisalahammad/794bf193b0112b06df077dc9b9608204 to your computer and use it in GitHub Desktop.
Save faisalahammad/794bf193b0112b06df077dc9b9608204 to your computer and use it in GitHub Desktop.
Use this jQuery script to automatically set the default selected option to "09" for hour dropdowns in Ninja Forms plugin Date/Time field. This code waits until elements with class `.setdefaulttime` appear on the page, then targets `<select>` elements with IDs starting with `hour-select-` or class `.hour.extra` and selects the option with value "…
/**
* Set default hour to 09:00 for date/time fields in Ninja Forms
*
* @author Faisal Ahammad <[email protected]>
*
* First add the "setdefaulttime" CSS class in the Date/Time → Display → Container to make this code work.
*/
jQuery(document).ready(function($) {
function checkAndSetHour() {
if ($('.setdefaulttime').length === 0) {
setTimeout(checkAndSetHour, 100);
return;
}
var $select = $('select[id^="hour-select-"], select.hour.extra');
$select.each(function() {
$(this).find('option[value="09"]').prop('selected', true);
});
}
checkAndSetHour();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment