I've searched the web and every tutorial I can find says the same thing, and I am doing what they say, but it's inexplicably not working.
Basically, I'm using a nested object to populate a select box using the new Option() method. Unfortunately, when I include the new Option line, it breaks the loop and does not add anything...
HTML Code:
// note: mySelectBox = document.new_issue.upgradeid;
// clear select box
mySelectBox.length = 0;
// and re-populate...
for (var key in upgradeprogArray[myGame])
{
var currentIndex = mySelectBox.length;
var currentValue = upgradeprogArray[myGame][key];
if (currentValue != '')
{
alert('mySelectBox.option['+ currentIndex +'] = new Option('+ currentValue +', '+ key +')');
// mySelectBox.option[currentIndex] = new Option(currentValue, key);
}
}
When this is run, it traces each item that it would have added and all the data passed to the alert box is accurate.
Unfortunately, when that last line is NOT commented out, it only executes the loop once and then breaks without adding any options to my select box.
If upgradeprogArray[myGame] is not an object (an unordered array), but an array, you should use the ordered for() loop (specific to the arrays), not an unordered loop (which in JavaScript is used for objects).
The reason is simple. In JavaScript all the elements, variables, etc are objects. Native objects have a lot of native properties. An Array is also an object, and, besides his elements (which are custom properties), it has some other "technical", native properties, such as length. An unordered loop will include those native properties along the elements, which is not what you need.
omg the solution was actually something simple ><;;
Moral: check syntax carefully -_-
Thank you, Fang, I really appreciate it.
If you've got a moment, I'm completely stumped with another problem over in another thread: Very simple method doesn't work reliably. I hope more eyes may see something I missed.
If upgradeprogArray[myGame] is not an object (an unordered array), but an array, you should use the ordered for() loop (specific to the arrays), not an unordered loop (which in JavaScript is used for objects).
With the addition of the missing s on options, the scrips works perfectly.
I tried defining upgradeArray as an object using both these methods:
$js_content.= "
var upgradeprogArray = new Array();";
foreach ($upgradeprogArray as $game => $subArray)
{
$js_content.= "
upgradeprogArray[\"".$game."\"] = new Array();";
var upgradeprogArray = new Array();
var upgradeprogArray['game1'] = new Array();
var upgradeprogArray['game1']['key'] = 'val';
var upgradeprogArray['game1']['key'] = 'val';
etc...
var upgradeprogArray['game2'] = new Array();
var upgradeprogArray['game2']['key'] = 'val';
etc...
Which is more important: To write a few extra code lines? Or to use the correct syntax?
Now: does, or does not your code work?
It's most important that my code work reliably.
Being that these are 2 threads, I should point out that these problems are coming from 2 completely different files. Yes, they belong to the same project, but the errors are completely different.
This problem was fixed when I added that missing s to "options"
The other problem will be fixed as soon as I track down the source.
Bookmarks