add values to dropdown list

S

soni2926

hi,
I'm trying to add some values to a dropdown list via javascript, but i
keep getting this error:
sControl.options is undefined

here is the code below:
var sControl = document.getElementById
('ctl00_ContentPlaceHolder1_ddl2');
var totalSize = result.length;

var opt = document.createElement("option");

for (var i = 0; i < totalSize; i++) {
sControl.options.add(opt);
opt.text = "something";
opt.value = "123";
}

if i use firebug (firefox js debugger plugin) it shows that it picks
up sControl as type select-one, am i adding it wrong to the
dropdownlist?
 
T

Thomas 'PointedEars' Lahn

soni2926 said:
I'm trying to add some values to a dropdown list via javascript, but i
keep getting this error:
sControl.options is undefined

here is the code below:
var sControl = document.getElementById
('ctl00_ContentPlaceHolder1_ddl2');

Looks like a rather clueless approach to get a form control.
Brush up on the `elements' collection (discussed here ad nauseam).
var totalSize = result.length;

Good idea, however you should consider to declare more than one loop-related
variable in the initialization part of the `for' statement:

for (var i = 1, j = 2, k = 3; ...; ...)

That is not related to block scoping but it shows more clearly where a
variable is used.
var opt = document.createElement("option");

Not so good an idea. Use the proprietary, but better supported

var opt = new Option("text", "value");

instead.
for (var i = 0; i < totalSize; i++) {
sControl.options.add(opt);


See above. Do it the standards-compliant *and* backwards-compatible way
instead:

var opts = sControl.options;
opts[opts.length] = opt;

instead.
opt.text = "something";
opt.value = "123";

Although it could work, this statement order makes little sense. I
recommend (with exceptions) to assign the properties first, then append the
child element (object).
}

if i use firebug (firefox js debugger plugin) it shows that it picks
up sControl as type select-one, am i adding it wrong to the
dropdownlist?

Probably your markup is not Valid. A `select' element MUST contain at least
one `option' element in the first place.

<http://validator.w3.org/>


PointedEars
 
S

SAM

Le 11/24/08 10:02 PM, soni2926 a écrit :
hi,
I'm trying to add some values to a dropdown list via javascript, but i
keep getting this error:
sControl.options is undefined

here is the code below:
var sControl = document.getElementById('ctl00_ContentPlaceHolder1_ddl2');

and there, at this step :
alert(sControl);
does it say 'undefined' ?

if yes, probably your select has no ID ?!
var totalSize = result.length;

var opt = document.createElement("option");

for (var i = 0; i < totalSize; i++) {

sControl.options = new Option("something", "123");
}
sControl.options.add(opt);
opt.text = "something";
opt.value = "123";
}

if i use firebug (firefox js debugger plugin) it shows that it picks
up sControl as type select-one,


and then ?
that doesn't tell if it has an ID ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top