473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Replace all values in a Listbox

I have used
onChange="ReplaceValues(this.options[this.selectedIndex].value)" to
change several textbox values when a user makes a selection from a
Select List.

Now I have the requirement to replace all the values in another Select
List when a selection is made on the first Select List.

There are two questions: What is the Javascript syntax for deleting and
replacing option values in a Select List and how would you (how should
I ) store the data that I will be using to repopulate an existing
Select List.

Thanks,
IanO

Apr 28 '06 #1
1 1968
Ia*****@gmail.com wrote:
I have used
onChange="ReplaceValues(this.options[this.selectedIndex].value)" to
change several textbox values when a user makes a selection from a
Select List.
Only that there is no such built-in method. Which is why this information
is of little value to other people.
Now I have the requirement to replace all the values in another Select
List when a selection is made on the first Select List.
You are about the 4711th person to ask this here. Search the archives.
There are two questions: What is the Javascript syntax for deleting and
replacing option values in a Select List
- Deleting an option:

delete selectRef.options[index];

If that does not work:

selectRef.options[index] = null;

- Shortcut for deleting all options: selectRef.options.length = 0;
Fall back to a loop of the previous if that does not work.

- Replace option value:

selectRef.options[index].value = "...";

- Add new option:

selectRef.options[selectRef.options.length] = new Option(...);
and how would you (how should I ) store the data that
I will be using to repopulate an existing Select List.


This has been asked before, too. One possibility is:

var data = {
sel1Value1: [
{text: ..., value: ...},
...
],

sel1Value2: [
...
],

...
};

and then:

<script type="text/javascript">
function repopulate(oSrc, sTgt)
{
var oTgt = oSrc.form.elements[sTgt];
if (oTgt)
{
// TODO: delete all options of oTgt;
// maybe it is more efficient to overwrite existing options
// and delete only those that are no longer needed

var
curVal = oSrc.options[oSrc.selectedIndex],
curData = data[curVal];

for (var i = 0, len = curData.length; i < len; i++)
{
// TODO: add new option using properties of curData[i]
}
}
}

<select ... onchange="repopulate(this, 'sel2');">

The data is best generated server-side, by a server-side fallback to handle
the case that client-side script or DOM support is not available.
PointedEars
--
There are two possibilities: Either we are alone in the
universe or we are not. Both are equally terrifying.
-- Arthur C. Clarke
Apr 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zhang Le | last post by:
Hello, Is there a quick way to replace the content of a single item in tkinter's listbox? Currently my solution is to first delete the item, then insert a new item at the same position. I think...
7
by: Dave Hopper | last post by:
Hi I posted a question recently regarding problems I am having getting a value from a list box to use in a query. I got a lot of help, for which I thank you and it's nearly working! But I need...
6
by: Janaka | last post by:
Help! I have two ListBox controls on my web form. The first one gets populated on entry with values from the DB. I then use JavaScript to copy options from this ListBox to my second one. (I...
0
by: Dave | last post by:
Hi all, I have a listbox that is complex bound by an arraylist. The problem is that when I delete an object from the arraylist, the listbox does not reflect those changes. I tried refreshing...
7
by: technocraze | last post by:
Hi guys, I encountered this error while using the AfterUpdate event for my listbox. Error: Update or CancelUpdate without using AddNew or Edit. What i wanted to achieve is just to display the...
1
by: Intrepid_Yellow | last post by:
Hi, I have the following code that runs my report generator. The user selects a table from a combo box, then whatever fields they want from a list box. (This part all works and the report runs...
1
by: melvincwmah | last post by:
I have a form in which I would link the form fields to tables in the SQL database. Right now, I want to put in a listbox that will show three columns from a table called IssueAudit. The...
3
by: Renilkumar | last post by:
Hi, I am using .net 2.0. I have a .aspx page with vb.net as codebehind. My form has one dropdown, 2 listboxes with add & remove button. During pageload I am loading all the values from the db to...
10
by: angelicdevil | last post by:
ok i have a list of data that gets displayed in listbox... i want tht when i select any value in tht listbox and click on rename it should rename tht in the database with the value i provide in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.