/    Sign up×
Community /Pin to ProfileBookmark

Setting the default selected element in dynamic select drop-down box

Hi, I have created a dynamically generated drop-down box. I am struggling to find the syntax so that I can specify one of the select elements to be selected as default.

In normal html i would just write: <select selected=”selected”>Test</select>

I’m using javascript to create the <select> elements like this:

obj.options[obj.options.length] = new Option(‘testName’,’0000′);n

This is probably simple, but please could someone show how i could make this the default selected element?

Thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameFeb 04.2009 — obj.options[obj.options.length] = new Option('testName','0000');

obj.options[obj.options.length-1].selected = 'selected'; // = true works also
Copy linkTweet thisAlerts:
@coldscooterauthorFeb 04.2009 — Thank you so much. Works a treat ;-)
×

Success!

Help @coldscooter 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 4.27,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...