populate listbox using Javascript

R

RA

I tried following :
document.getElementById("lsTest").Items.Add(new ListItem("Test"))

This gives following error message:
'document.getElementById(...).Items' is null or not an object

Also tried:
document.getElementById("lsTest").Add(new ListItem("Test"))
Result - Error Message:
'listitem' is undefined

Any idea?
 
B

bruce barker

var o = document.createElement("option");
o.text = "test"
o.value = "test";
document.getElementById("lsTest").add(o);

-- bruce (sqlwork.com)



| I tried following :
| document.getElementById("lsTest").Items.Add(new ListItem("Test"))
|
| This gives following error message:
| 'document.getElementById(...).Items' is null or not an object
|
| Also tried:
| document.getElementById("lsTest").Add(new ListItem("Test"))
| Result - Error Message:
| 'listitem' is undefined
|
| Any idea?
|
|
 
K

Kumar Reddi

Hi RA,
you can not add a listitem from javascript. A ListBox is rendered as
html SELECT element. So, you should add the items in javascript as if you
are adding to a SELECT element, which would be like follows

document.getElementById("lsTest").add(new Option("Text","Value"));

HTH
Kumar
 
R

RA

To remove an item from a textbox I tried;

document.getElementById("lsTest").remove(new Option("Testing","Testing"));
and
document.getElementById("lsTest").remove("Testing");

neither of above work. Both deletes very first Item in the listbox.

What I am doing wrong?
 
Joined
Aug 5, 2007
Messages
1
Reaction score
0
Hi RA,

Try the below code inside the loop:

document.getElementById('lstbxStatus').remove([index]);

Srijith.
 

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,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top