Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Mutable list box + grouping function = Ctrl key buggy

Reply
Thread Tools

Mutable list box + grouping function = Ctrl key buggy

 
 
radio
Guest
Posts: n/a
 
      07-21-2004
I have a list box populated with some groups. All groups have a list
of related offices; I have these in the following xml format:
<Location>My Group 1</Location>
<Location Group="My Group 1">Office 1</Location>
<Location Group="My Group 1">Office 2</Location>
<Location>My Group 2</Location>
<Location Group="My Group 2">Office 1</Location>
<Location Group="My Group 2">Office 2</Location>

I have the following select box:
<select name="audienceLocation" size="6" multiple
onChange="tellGroup(this.options[selectedIndex].text);" >
</select>

Which calls the below function and finds a match with <Location> and
the Group element. So effectively if you click My Group 1, all of the
offices that are associated with My Group 1 are also selected.

function tellGroup(name)
{
var elements = (controlledVocabXml.documentElement.getElementsByT agName("Location"));
var audienceUnencoded = "";
for (j=0;j<[elements.length];j++)
{
var audienceElements = (elements[j].Group);
var audienceRegExp = new RegExp(audienceElements + "(,|$)+");
if (name.match(audienceRegExp) != null)
{document.theForm.audienceLocation.options[j].selected = true; }
}
}


This works, however, the problem I'm having is if the Ctrl key is held
while selecting another group (so the users can select multiple
groups) the function doesn't seem to work on the newly selected group.
I'm restricted to DOM level 1 as I'm writing code that has to work in
Dreamweaver's API.

Any help much appreciated
 
Reply With Quote
 
 
 
 
Joakim Braun
Guest
Posts: n/a
 
      07-21-2004
"radio" <ra-> skrev i meddelandet
news: om...
> I have a list box populated with some groups. All groups have a list
> of related offices; I have these in the following xml format:
> <Location>My Group 1</Location>
> <Location Group="My Group 1">Office 1</Location>
> <Location Group="My Group 1">Office 2</Location>
> <Location>My Group 2</Location>
> <Location Group="My Group 2">Office 1</Location>
> <Location Group="My Group 2">Office 2</Location>
>
> I have the following select box:
> <select name="audienceLocation" size="6" multiple
> onChange="tellGroup(this.options[selectedIndex].text);" >
> </select>
>
> Which calls the below function and finds a match with <Location> and
> the Group element. So effectively if you click My Group 1, all of the
> offices that are associated with My Group 1 are also selected.
>
> function tellGroup(name)
> {
> var elements =

(controlledVocabXml.documentElement.getElementsByT agName("Location"));
> var audienceUnencoded = "";
> for (j=0;j<[elements.length];j++)
> {
> var audienceElements = (elements[j].Group);
> var audienceRegExp = new RegExp(audienceElements + "(,|$)+");
> if (name.match(audienceRegExp) != null)
> {document.theForm.audienceLocation.options[j].selected = true; }
> }
> }
>
>
> This works, however, the problem I'm having is if the Ctrl key is held
> while selecting another group (so the users can select multiple
> groups) the function doesn't seem to work on the newly selected group.
> I'm restricted to DOM level 1 as I'm writing code that has to work in
> Dreamweaver's API.
>
> Any help much appreciated


Rewrite tellGroup to check for all options of audienceLocation whose
..selected is true.

selectedIndex is a single index into the options array, indicating the first
selected option. You can't "multiple-access" array elements with it.

Joakim Braun


 
Reply With Quote
 
 
 
 
Joakim Braun
Guest
Posts: n/a
 
      07-21-2004
> "radio" <ra-> skrev i meddelandet
> news: om...


<snip>

> > I have the following select box:
> > <select name="audienceLocation" size="6" multiple
> > onChange="tellGroup(this.options[selectedIndex].text);" >
> > </select>

<snip>

Also, this won't work very well if nothing is selected (selectedIndex
is -1).

Joakim Braun



 
Reply With Quote
 
radio peep
Guest
Posts: n/a
 
      07-21-2004
Thanks Joakim, I’m a bit new to JavaScript. I’m gonna have a go at
Rewriting tellGroup (wish me luck) but I’m a bit stuck on how to make
this (onChange="tellGroup(this.options[selectedIndex].text);") not
through errors when nothing is selected. Got any suggestions here?

Many thanks




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Joakim Braun
Guest
Posts: n/a
 
      07-21-2004
"radio peep" <ra-> skrev i meddelandet
news:40fe4488$0$16480$.. .
> Thanks Joakim, I'm a bit new to JavaScript. I'm gonna have a go at
> Rewriting tellGroup (wish me luck) but I'm a bit stuck on how to make
> this (onChange="tellGroup(this.options[selectedIndex].text);") not
> through errors when nothing is selected. Got any suggestions here?
>
> Many thanks
>


Array access with a -1 index generates an error. So you must check for
selectedIndex==-1 first. So instead of accessing the array directly in the
onchange="...", you could do all the work in your tellGroup() function:

// Untested code, but this should be simple enough

var selIndex = document.someForm["someSelect"].selectedIndex;

if(selIndex > -1){

var theOptions = document.someForm["someSelect"].options;

for(var i = 0, max = theOptions.length; i < max; i++){

if(theOptions[i].selected){
// This option is selected - do stuff with it
}
}
}
else{
// do other stuff here if the previous selection has become unselected
}

Joakim Braun


 
Reply With Quote
 
radio
Guest
Posts: n/a
 
      07-22-2004
Thanks Joakim, with your help i'm pretty sure i've sorted it out.

cheers

Radio



> Array access with a -1 index generates an error. So you must check for
> selectedIndex==-1 first. So instead of accessing the array directly in the
> onchange="...", you could do all the work in your tellGroup() function:
>
> // Untested code, but this should be simple enough
>
> var selIndex = document.someForm["someSelect"].selectedIndex;
>
> if(selIndex > -1){
>
> var theOptions = document.someForm["someSelect"].options;
>
> for(var i = 0, max = theOptions.length; i < max; i++){
>
> if(theOptions[i].selected){
> // This option is selected - do stuff with it
> }
> }
> }
> else{
> // do other stuff here if the previous selection has become unselected
> }
>
> Joakim Braun

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The richtextbox not receiving the keys such Ctrl+A, Ctrl+O when typedon it when hosted on web page Gouri.Mahajan7@gmail.com ASP .Net 0 07-11-2008 05:27 AM
Disabling the shortcuts such as Ctrl+A, Ctrl+B.... using Javascript Gouri.Mahajan7@gmail.com ASP .Net 2 07-10-2008 08:15 AM
How to intercept Ctrl key - eg Ctrl E being pressed on Java console program Angus Java 5 11-18-2006 04:19 PM
Implement Ctrl-C, Ctrl-V Danny C++ 5 08-15-2003 03:04 AM
Implement Ctrl-C, Ctrl-V Danny C Programming 5 08-15-2003 03:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57