I wrote a script for populating a selectbox with a bunch of options.
Initially the data
is in the form of string of a format "key=value;key2=value2;etc...":
//split the string to distinguish between different options to populate a selectbox with
var values = data.split(';');
//reset the length of the selectbox to be populated
document.getElementById(child).options.length = 0;
//create first default option
document.getElementById(child).options[0] = new Option('all', '0');
for(var i = 0; i < values.length; i++){
//check for and remove unnecessary characters
values[i].replace(/\s+/g, '');
//split the option to get the key and value separately
var options = values[i].split('=');
if(!isEmpty(options[0]) && !isEmpty(options[1])){
//insert a new element to the selectbox
document.getElementById(child).options[i+1] = new Option(options[1], options[0]);
}
}
The example above populates a selectbox with the given html output:
<option value="0">all</option>
<option value="
7">Bermuda</option>
<option value="10">British Virgin Islands</option>
<option value="15">Cayman Islands</option>
<option value="42">Jamaica</option>
<option value="74">St. Lucia</option>
<option value="79">Trinidad Tobago</option>
As you can notice above the second option in the selectbox has a corrupted string value. I need to fix that value because because of that cake cannot save this value properly.
If you have any other questions please ask.
\n
) – MMM 20 hours ago