I have a section of javascript which i'm trying to use to dynamically generate the options for a mult-select box, when another box's contents are changed. Basically i have a method that returns an array of arrays, where each array is of the form
[name, number]
I then iterate over the arrays, and substitute in the values out of the array -
<%options_for_select(SubjectFamilyMember.options_for_select(["3"])).each_with_index do |option_tag, i| -%> $("subject_family_members").options[<%= i+1.to_i %>]=new Option('<%= option_tag[0] %>', <%= option_tag[1].to_i %>, false, false); <%end %>
When i run this in the console (escaping the quotes), it seems to produce the right results -
$("subject_family_members").options[1]=new Option("All", 0, false, false); $("subject_family_members").options[2]=new Option("Violin", 13, false, false); $("subject_family_members").options[3]=new Option("Viola", 14, false, false); $("subject_family_members").options[4]=new Option("Cello", 15, false, false); $("subject_family_members").options[5]=new Option("Double Bass", 16, false, false);
However, when it's run on the page, all the strings come out as "60" and all the numbers come out as "111" - i see this javascript when i look at the page source -
$("subject_family_members").options[1]=new Option('60', 111, false, false); $("subject_family_members").options[2]=new Option('60', 111, false, false); $("subject_family_members").options[3]=new Option('60', 111, false, false); $("subject_family_members").options[4]=new Option('60', 111, false, false); $("subject_family_members").options[5]=new Option('60', 111, false, false);
So, the 'i' out of my 'each_with_index' is coming through fine, but the array elements are being broken somehow. Does anyone know what might be going wrong here?
thanks max