Forum Moderators: open

Message Too Old, No Replies

Selected item in drop down

Wrong item select ... add an alert to debug the correct item gets selected!

         

JoeyO

12:49 pm on May 10, 2007 (gmt 0)

10+ Year Member



Hi folks, I have a script that dynamically adds options to a select box. The items are just numbers. I have a for loop that uses a value from another control as a starting point and yet another var to match on to show witch item s/b "selected". When the page is loaded the function gets called and the selected item is always one less than it s/b. I put an alert in the funtion to display the vars and when the page is loaded and after the alert shows the vars, the drop down has the correct value! I take the alert out and it is wrong again! That makes debugging quite a challenge. Here is the function ... if I put an alert anywhere in there the correct item gets selected ... otherwise the item seleted is one less than it should be ...

function updateDay2_2()
{
var x2=document.getElementById("theSecondDay")
var y2=x2.value
var x=document.getElementById("dayTwo")
var selectIndex = document.getElementById("dayOne").selectedIndex
numOptions = x.options.length
for (i=0;i<numOptions;i++)
{
x.remove(x.i)
}
if (selectIndex > 0 ¦¦ selectIndex == 1)
{
selectIndex = selectIndex + 1
}
if (selectIndex == 0)
{
selectIndex = 1
}
for (i=selectIndex;i<28;i++)
{
var y=document.createElement('option');
y.text=i+1
if ( y.text == y2 )
{
y.selected='true'
}
x.add(y)
}
}

geofflee

3:38 am on May 11, 2007 (gmt 0)

10+ Year Member



Certain browsers do not allow you to modify the DOM until the page has been completely loaded. If I remember correctly, this is the case for Firefox. This makes sense because you can't manipulate something before it exists. The window.onload event will only invoke after everything in the window has been loaded, including pictures, so give that a try.

-Geoffrey Lee