Year Select
year_select outputs an HTML <select> containing a year range. It is commonly used for card-expiration year inputs and other checkout/account forms where you need a controlled year list.
See also:
Parameters
years
Number of years to include in the dropdown.
Default: 5.
If non-numeric, CartThrob falls back to 5.
years="5"
start_year
First year in the generated range.
Default: current year (date('Y')).
If non-numeric, CartThrob falls back to the current year.
start_year="2026"
selected
Selected year value.
Default: current year (date('Y')).
selected="{customer_expiration_year}"
name
HTML name attribute for the generated <select>.
Default: expiration_year.
name="expiration_year"
extra
Raw extra attributes appended to the <select> output.
extra='rel="my_attr" title="my title"'
onchange
Optional JavaScript to run when the select value changes.
onchange="your_custom_javascript()"
class
CSS class attribute for the generated <select>.
class="year-select"
id
CSS/DOM id attribute for the generated <select>.
id="expiration_year"
Output
Returns a complete <select> element built with years from:
start_year- through
start_year + years - 1
Example: start_year="2026" and years="5" outputs 2026, 2027, 2028, 2029, 2030.
Behavior and Constraints
- This is an immediate-execution single tag.
- Year range is generated in ascending order (
for ($year = $start_year; $year < $start_year + $years; $year++)). yearsandstart_yearare numeric-checked; invalid values use defaults.- If
yearsis0(or negative), no options are generated. - This tag only renders the dropdown; it does not save or validate submitted values by itself.
Examples
Default range from current year:
{exp:cartthrob:year_select}
Installer/stub pattern:
{exp:cartthrob:year_select years="6"}
Custom start year and selected value:
{exp:cartthrob:year_select
name="expiration_year"
start_year="2026"
years="10"
selected="{customer_expiration_year}"}
With HTML attributes:
{exp:cartthrob:year_select
id="expiration_year"
class="form-control"
onchange="updateCardPreview()"
extra='data-test=\"exp-year\"'}