1

I've been having some trouble adding options to a chart.js <select> from an array of <option>s. I used the same rough procedure all the time, so I'm not sure why it's failing me now...

What I'm doing:

  1. I have several <select>s which look more or less like this:
<select id='soilname' data-placeholder='Type here' multiple class='chosen-select'>
    <option value=''></option>
</select>
  1. A sequence of steps that gets me to an array of <option>s. This array is called optnames.
  2. I iterate through the array, adding it to several <select>s:
for(var i=0; i < optnames.length; i++)
{
    document.getElementById("soilname").appendChild(optnames[i]);
    document.getElementById("soilyearname").appendChild(optnames[i]);
    document.getElementById("albedoname").appendChild(optnames[i]);
    document.getElementById("dcname").appendChild(optnames[i]);
    document.getElementById("acname").appendChild(optnames[i]);
}
  1. Some distance later, I activate chosen.js:
jQuery(".chosen-select").chosen({
    width: "400px"
});

I made sure to include all relevant files and plugins in <script> tags. I've also console.log()ed optnames to make sure the array is what I expect it to be. The several other pages on this very topic propose good solutions -- none work. Any thoughts as to what the error could be?

EDIT: some screenshots:

  1. The search boxes. They are correctly sized to 400px. search boxes upon loading

  2. Clicking one of the text boxes. I added a dummy option manually here. What's going wrong here?

bsp2121
  • 11
  • 3

1 Answers1

0

I managed to figure out what was going wrong. I was mixing jQuery and JavaScript (something I'm in the bad bad habit of doing rather often). I've now written the code such that it is all jQuery:

for(var i=0; i < mynames.length; i++)
{
    $('#soilname').append($("<option>"+mynames[i]+"</option>"));
    $('#soilyearname').append($("<option>"+mynames[i]+"</option>"));
    $('#albedoname').append($("<option>"+mynames[i]+"</option>"));
    $('#dcname').append($("<option>"+mynames[i]+"</option>"));
    $('#acname').append($("<option>"+mynames[i]+"</option>"));
}

(mynames is the array of strings from which I was originally building optnames.) This added all the options exactly as I wanted them. I hope others may find this useful.

bsp2121
  • 11
  • 3