I’m working on customizing <select>‘s. But I got stuck behind a problem. A quick console.log(options); shows the array’s result:
[undefined, undefined, “Selena Gomez”, “Mila Kunis”, undefined, undefined, undefined, undefined, undefined, undefined, undefined, “Miley Cyrus”]
Also when I do alert(options.length); the result is 12. When in reality there are 4 options in the select..
Code that produces the array:
var options = new Array;
$(this).children('option').each(function () {
if ($(this).val()) {
options[$(this).val()] = $(this).text();
}
});
console.log(options);
I have no clue what is the issue.. I added the if ($(this).val()), to be sure, that no static gets in the array, but still.
Note: However, when I open the array up in firebug, it only shows the correct entries.
$(this).val()must not be 0, 1, 2, 3 then, but rather 2, 3, 11 (where is the 4th?)Use
options.push($(this).text());instead or use it as an object to avoid auto generation of missing indexes.Also,
$(this).val()will evaluate tofalseif it is empty""or0, is that the 4th one perhaps?