/    Sign up×
Community /Pin to ProfileBookmark

Set selected item true

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

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@KorJan 14.2004 — 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 [b]text[/b] instead of [b]value[/b]

Try this:

for(i = 0; i <= document.getElementById(dropdownName).options.length; i++){

if(document.getElementById(dropdownName).options[i].[color=red]text[/color] == 'content'){


document.getElementById(dropdownName).options[i].selected = true;

}else{}

}
Copy linkTweet thisAlerts:
@vrachosauthorJan 14.2004 — [i]Originally posted by Kor [/i]

[B]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 [b]text[/b] instead of [b]value[/b]



Try this:



for(i = 0; i <= document.getElementById(dropdownName).options.length; i++){

if(document.getElementById(dropdownName).options[i].[color=red]text[/color] == 'content'){


document.getElementById(dropdownName).options[i].selected = true;

}else{}

}
[/B]
[/QUOTE]




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



But thnx for youre reply
Copy linkTweet thisAlerts:
@PittimannJan 14.2004 — 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
Copy linkTweet thisAlerts:
@vrachosauthorJan 14.2004 — Thnx Pittimann

This removes the error indeed.
Copy linkTweet thisAlerts:
@PittimannJan 14.2004 — 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
×

Success!

Help @vrachos spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...