Click to See Complete Forum and Search --> : Set selected item true


vrachos
01-14-2004, 02:35 AM
I'm currently working on a project where i'm making a select box. then i do a for loop on the selectbox so i can set a selected item(this is stored in a database and parsed to the function).

------The Code-------------------

fieldCounter++;
dropdownName = numtoChar(fieldCounter-1);
dropdownName = dropdownName + "int_chapter_id";

for(i = 0; i <= document.getElementById(dropdownName).options.length; i++){
if(document.getElementById(dropdownName).options[i].value == content){ //here is the error
document.getElementById(dropdownName).options[i].selected = true;
}else{}
}

-------End of the code-------------

I've search for the error in the script(tested with commenting the rest out) and i think the error is in the if statment.
The error I get is: 'document.getElementById(...).options[...].value' is empty or not an object.
The script however work fine exept for that is displays the error.
Above this script is a piece of script that created a new row in a table and a cell with the selectbox is this maybe the problem. Or is it something else.

I'm getting the error in Internet Explorer I haven't tested it on other browsers yet

Kor
01-14-2004, 03:00 AM
Maybe the error occured because the input's value and the option's value are not the same...

If you want to refere the content of the option use text instead of value

Try this:

for(i = 0; i <= document.getElementById(dropdownName).options.length; i++){
if(document.getElementById(dropdownName).options[i].text == 'content'){
document.getElementById(dropdownName).options[i].selected = true;
}else{}
}

vrachos
01-14-2004, 03:07 AM
Originally posted by Kor
Maybe the error occured because the input's value and the option's value are not the same...

If you want to refere the content of the option use text instead of value

Try this:

for(i = 0; i <= document.getElementById(dropdownName).options.length; i++){
if(document.getElementById(dropdownName).options[i].text == 'content'){
document.getElementById(dropdownName).options[i].selected = true;
}else{}
}

This isn't working i still get the error and the script doesn't work anymore.

But thnx for youre reply

Pittimann
01-14-2004, 03:10 AM
Hi!

Well - I think, content is a variable set somewhere else.

As vrachos stated, that the script works (except displaying the error).

The error comes from the loop itself. The line:
for(i = 0; i <= document.getElementById(dropdownName).options.length; i++){

makes the loop run 1 time too often because of the "<=".

This should be "<" instead...

Cheers - Pit

vrachos
01-14-2004, 03:13 AM
Thnx Pittimann
This removes the error indeed.

Pittimann
01-14-2004, 03:17 AM
Hi!

You're welcome!

Such tiny things also happen to me, not because of not knowing, rather because of being concentrated in something else already while typing. Just yesterday I posted the same thing for somebody, where "<=" had to be "<" :rolleyes:

Cheers - Pit