473,785 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Select List Contents

I have a select list defined like:

<select name="cri2" multiple style="height:2 20px; width:300px;"
onClick="show_c ontent(this);">
</select>

I add content to it dynamically using a function like:

function addOption(theSe l, theText, theValue, optionColor)
{
var newOpt = new Option(theText, theValue);
var selLength = theSel.length;
theSel.options[selLength] = newOpt;
theSel.options[selLength].style.color = optionColor;
}

Sometimes the text content is so large that I need the user to be able
to see it using the onclick event in the select like:

function show_content(ob j)
{
//this did not work i got an error about the object not
supporting this property or method
//alert(obj.optio ns[obj.selectedInd ex].value);
for ( var i=0; i<obj.length; i++ )
{
if (obj[i].selected){aler t(obj[i].value);break;}
}
}

The pointer seems all screwed up. the first time i click on it i get
nothing. then the next time i get the alert. If there are 3 items the
alert is for the last item i clicked on.

Any help is appreciated.

Mike

Jan 19 '06 #1
1 4027
mike wrote:
I have a select list defined like:

<select name="cri2" multiple style="height:2 20px; width:300px;"
onClick="show_c ontent(this);">
</select>
IE fires the onclick event before it sets the option to selected, so you
don't get the one that was just clicked on. I think it expects that
the first click is on the drop-down arrow not one of the options.


I add content to it dynamically using a function like:

function addOption(theSe l, theText, theValue, optionColor)
{
var newOpt = new Option(theText, theValue);
var selLength = theSel.length;
theSel.options[selLength] = newOpt;
theSel.options[theSel.options. length] = newOpt;

Remove the 'var selLength = ...' line.

theSel.options[selLength].style.color = optionColor;
Test for support for the style object, you already have a reference to
the option, why get it again?

if (newOpt.style) {
newOpt.style.co lor = optionColor;
}
}

Sometimes the text content is so large that I need the user to be able
to see it using the onclick event in the select like:

function show_content(ob j)
{
//this did not work i got an error about the object not
supporting this property or method
//alert(obj.optio ns[obj.selectedInd ex].value);
for ( var i=0; i<obj.length; i++ )
{
if (obj[i].selected){aler t(obj[i].value);break;}
}
}


Once the wrapping error from your comment is removed, it 'works' for me.
A more efficient routine is:

function show_content(ob j)
{
var op, s = [];
for (var i=0, len=obj.options .length; i<len; ++i) {
op = obj.options[i];
if (op.selected) s[s.length] = op.text;
}
alert(s.join('\ n'));
}
Here's a working example:

<script type="text/javascript">

function addOption(theSe l, theText, theValue, optionColor)
{
var newOpt = new Option(theText + '-' + theSel.length,
theValue + '-' + theSel.length);
theSel.options[theSel.options. length] = newOpt;
newOpt.style && (newOpt.style.c olor = optionColor);
}

function show_content(ob j)
{
var op, s = [];
for (var i=0, len=obj.options .length; i<len; ++i)
{
op = obj.options[i];
if (op.selected) s[s.length] = op.text;
}
alert(s.join('\ n'));
}

</script>

<input type="button" value="Add one"
onclick="addOpt ion(document.ge tElementById('c ri2'),
'theText', 'theValue', 'red')">

<select name="cri2" id="cri2"
multiple style="height:2 20px; width:300px;"
onchange="show_ content(this);" >
</select>
[...]
--
Rob
Jan 20 '06 #2

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

Similar topics

13
2897
by: mr_burns | last post by:
hi, is it possible to change the contents of a combo box when the contents of another are changed. for example, if i had a combo box called garments containing shirts, trousers and hats, when the user selects shirts another combo box called 'size' would contain sizes in relation to shirts (ie. chest/neck size). the same would occur for trousers and hats. when the user selects an option in the garment combo box, the options available...
4
4673
by: Pasquale | last post by:
Hello, I wondering if there is a way to dynamically update a select list with javascript from a database query without having to reload the page to update the list?
1
5705
by: dschectman | last post by:
I have an interesting issue. I need to implement a dynamic table to mimic a select list. Each time you double click from the master list, a row is added to the list of selected items. The list of selected items has a column header and differentiation between odd and even rows. This part is trivial. Clients have the ability to re-order the list of selected items. This is also trivial: I trap the onClick event for a row to select it. ...
1
17673
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
3
2057
by: pmud | last post by:
Hi, I have 2 drop down lists on an asp.Net page. The 1st contains alphabets. When the user selects an alphabet frm the first list, the second drop down list should be populated with names from the sql database, which begin with that alphabet. For this I used an sql data Adapter & created a data set. The user's selection in 1st list is passed as parameter to the sql statement.
7
3391
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
1
1916
by: Chris Lieb | last post by:
In a form, I have two select lists. One holds the available options. I have some buttons between them for moving items from one list to the other. When the form first loads, all of the items are in the available list and none are in the used list. Available Used --------------- --------------- |item1 | |item3 | |item2 | >> | | |item4 | > | ...
23
7418
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
3
18718
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however, working with complex reports is tricky Assumption: Reader of this article have basic knowledge of creating data reports. Creating a Parent-Child Command and create a DataReport Suppose we have a database called company with two tables ...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.