-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.select_options.coffee
More file actions
48 lines (42 loc) · 1.77 KB
/
Copy pathjquery.select_options.coffee
File metadata and controls
48 lines (42 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
###
Created by Kyle Ellman
https://github.com/kyleellman/jquery-select-options
###
(($, window) ->
# This function will add an option tag to the desired element, either a select menu or optgroup
# Required:
# selectMenu: element to which the option will be added
# value: value of the option
# text: display text of the option
# Optional:
# selected: value of the option to be selected. This will automatically select the option of the values match
addOption = (selectMenu, value, text, selected) ->
newOption = $('<option></option>').attr('value', value).text(text)
newOption.attr('selected', 'selected') if value == selected
selectMenu.append newOption
# This function will replace the options of a select menu
# Required:
# options: This can be an array of objects (example: {text: "display text", value: "the value"}),
# or a hash of arrays whose keys are optgroup names
# Optional:
# otherOptions: This is a hash of optional settings:
# - includeBlank: a blank option added to the top of the select menu
# - selected: The default selected option of the select menu
$.fn.selectOptions = (options, otherOptions = {}) ->
# Remove old options
@empty()
# Blank option
if otherOptions?.includeBlank
addOption @, '', otherOptions.includeBlank
# No optgroups
if options instanceof Array
for option in options
addOption @, option.value, option.text, otherOptions?.selected
# Optgroups
else
for groupName, groupOptions of options
newGroup = $('<optgroup></optgroup>').attr('label', groupName)
for option in groupOptions
addOption newGroup, option.value, option.text, otherOptions?.selected
@append newGroup
) jQuery, window