Created
September 12, 2012 21:35
-
-
Save brobertsaz/3710100 to your computer and use it in GitHub Desktop.
changing options_for_select from one selector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%table | |
%tr | |
%td | |
= f.label :share_type, 'Share Type', class: 'required' | |
%td | |
= f.select :share_type, @share_types, {}, class: 'share-type', id: 'selected_share_type' | |
%tr | |
%td | |
= f.label :share_expiration_date, 'Expiration Date', class: 'required' | |
%td#share_exp_dates | |
= f.select :share_expiration_date, @share_across_dates | |
:javascript | |
var share_across_options = #{@share_across_dates}; | |
var share_down_options = #{@share_down_dates}; | |
var optionsFromArray = function(array){ | |
var options = ''; | |
$(array).each(function(index, item){ | |
options += '<option value="' + item[1] + '">' + item[0] + '</option>' | |
}); | |
return options; | |
}; | |
var populateSubOptions = function(selectedOption) { | |
if (selectedOption == 'share_across') { | |
$('#share_exp_dates select').empty().append(optionsFromArray(share_across_options)); | |
} else { | |
$('#share_exp_dates select').empty().append(optionsFromArray(share_down_options)); | |
} | |
}; | |
var initSubOptions = function(){ | |
$('#selected_share_type').change(function() { | |
populateSubOptions($(this).val()); | |
}); | |
}; | |
$(document).ready(function(){ | |
initSubOptions(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment