I have a select list defined like:
<select name="cri2" multiple style="height:220px; width:300px;"
onClick="show_content(this);">
</select>
I add content to it dynamically using a function like:
function addOption(theSel, 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(obj)
{
//this did not work i got an error about the object not
supporting this property or method
//alert(obj.options[obj.selectedIndex].value);
for ( var i=0; i<obj.length; i++ )
{
if (obj[i].selected){alert(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
|
|
0
|
|
|
|
Reply
|
hillmw (77)
|
1/19/2006 10:58:32 AM |
|
mike wrote:
> I have a select list defined like:
>
> <select name="cri2" multiple style="height:220px; width:300px;"
> onClick="show_content(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(theSel, 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.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(obj)
> {
> //this did not work i got an error about the object not
> supporting this property or method
> //alert(obj.options[obj.selectedIndex].value);
> for ( var i=0; i<obj.length; i++ )
> {
> if (obj[i].selected){alert(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(obj)
{
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(theSel, theText, theValue, optionColor)
{
var newOpt = new Option(theText + '-' + theSel.length,
theValue + '-' + theSel.length);
theSel.options[theSel.options.length] = newOpt;
newOpt.style && (newOpt.style.color = optionColor);
}
function show_content(obj)
{
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="addOption(document.getElementById('cri2'),
'theText', 'theValue', 'red')">
<select name="cri2" id="cri2"
multiple style="height:220px; width:300px;"
onchange="show_content(this);">
</select>
[...]
--
Rob
|
|
0
|
|
|
|
Reply
|
RobG
|
1/20/2006 6:07:03 AM
|
|
|
1 Replies
76 Views
(page loaded in 0.061 seconds)
Similiar Articles: REXX and DB2 - Validating the SQL - comp.lang.rexx... have a REXX program that Displays a PANEL that list all ... prep would do a syntax check, but as to whether content is ... twit with DB2 and SQL with REXX - comp.lang.rexx SELECT ... access control and pool servers - comp.protocols.time.ntp ...... name of the file containing your static ntp.conf content ... etc ; rm ntp.conf ln $WKDIR/ntp.conf } select ... IP is technically "dynamic" but it's the kind of dynamic ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... truncate file - comp.lang.c++.moderated... off submitting a patch to boost, and emailing the lists ... on one that does not) Why does C++ support dynamic memory ... Truncate file [A] Adjust address range to fit > Select ... SolidWorks Sucks... - comp.cad.solidworksThe weldment cut list and it's corresponding balloons ... with the fact that the assembly needs to be dynamic with ... you can exclude them from the BOM using the BOM Contents ... sqlldr not recognizing a file without an extension on Solaris ...... treat the file as an external table and get the contents ... Zakharychev N-Networks, makers of Dynamic PSP(tm ... down - puzzling - comp.lang.xharbour... curf:=select ... json functions - comp.lang.xharbourYou should start with the C function list, and ... in how you handle the "fields that have array contents". ... to accept an array from php/mysql to create the dynamic ... Need a FORTRAN compiler for Win7 (or XP) - comp.lang.fortran ...... executables for all of the above OS'es; you select the ... enable- libgomp --enabl e-lto --enable-fully-dynamic ... refreshed--altho it probably doesn't matter for content ... Media Center: Advertising - comp.lang.java.programmer... electrochemicals, operating systems, utilities, utility, listing and contents ... panelsamtal, socialf=C3=B6rs=C3=A4kringen, hobby, riksdagsgru= ppen, urban dynamic ... Changing Select Content on the fly - JavaScript Kit- Your ...Changing Select element content on the fly. Changing a select element's ... in advance (such as 3 above), so you need a dynamic way to add new options to select. JavaScript Toolbox - Dynamic Option List - Matt KruseDescription: This library allows you to easily create select boxes whose contents depend on the value in a parent select box. It features: Default options to select ... 7/27/2012 2:47:59 PM
|