Select boxes to select sets

C

callmebill

I'm relatively new to javascript, and I'm trying to decide whether the
following (and if so, clues on how to do it):

I'd like to create two HTML multiple-select boxes. The first would be
a list of items, and the second would be a list of categories. By
clicking a category in the 2nd box, the members of that category (from
the first box) would be highlighted (indicated here by arrows). E.g.,

-- List of Nums -- -- Categories --
1 Odd
2 <-- Even <--
3 Prime
4 <--
5

Any help in this matter is greatly appreciated.
 
R

RobG

I'm relatively new to javascript, and I'm trying to decide whether the
following (and if so, clues on how to do it):

I'd like to create two HTML multiple-select boxes. The first would be
a list of items, and the second would be a list of categories. By
clicking a category in the 2nd box, the members of that category (from
the first box) would be highlighted (indicated here by arrows). E.g.,

-- List of Nums -- -- Categories --
1 Odd
2 <-- Even <--
3 Prime
4 <--
5

Any help in this matter is greatly appreciated.

You probably won't get many suggestions because your requirements are
not clear and you've made no attempt to present a solution.

You seem to want to select options in one list based on the selection
from another list. That has been covered numerous times in previous
posts, search for 'dynamic option list', there's a thread here:

<URL:http://groups.google.co.uk/group/co...mic+option+list&rnum=5&hl=en#79d89c8dd0f2e169>

An issue you haven't covered is how to determine the options to select,
here's some suggestions.


You can hard-code the results, something like:

If 'Odd' selected, select 1, 3, 5
If 'Even' selected, select 2, 4
If 'Prime' selected, select 1, 2, 3, 5

A second method is to formulate tests for each entry in the numbers list
that are based on the categories:

If 'Odd' selected, select entries where isOdd(num) is true.
isOdd() can be: return ( num%2 != 0 );

If 'Even' selected, select entries where isOdd(num) is false.
(this will select 0 as even, which it isn't so if it is possible
that zero will be one of the numbers, deal with it)

If 'Prime' selected, select entries where isPrime(num)
(where isPrime() is some function that tests for prime numbers)

Another method is to create objects for all the entries in the number
list. Each object can have properties for value, isPrime and isOdd.
Then iterate through them much like the above only test the property of
each object rather than using a function. Select the associated entry
in the list if object's value for the tested property is true.


<script type="text/javascript">

var numObj = new Object();
numObj.value = 6;
numObj.even = true;
numObj.prime = false;

function showNumObj()
{
alert(
'Value: ' + numObj.value
+ '\nEven?: ' + ((numObj.even)?'Yes':'No')
+ '\nPrime?: ' + ((numObj.prime)?'Yes':'No')
);
}
</script>

<input type="button" value="See numObj" onclick="showNumObj();"><br>


How you create the objects and assign values to the properties is, of
course, another story.

There are probably many other ways of achieving whatever it is you are
trying to do.
 
C

callmebill

Obviously I read the "How to ask smart questions" FAQ _after_ I posted
my question. Sorry about that.

So here is something that I came up with as a starting point. It
presents two list boxes: one has a list of numbers, and one has a list
of "groups". When a group is selected, the numbers in the first box
that are determined to be members of that group are highlighted.

The actual project I'm working on is quite a bit more complicated than
this, and would use Python to generate the list of Groups and the list
of Members, and to generate the JavaScript that determines the
relationships. It's all based on some XML that changes pretty
frequently.



----------------------------------------------
<script language="JavaScript">
<!--

function isPrime( val ){
if(val < 2){
return false;
}

else if(val == 2){
return true;
}

else{
for(var i = 2; i <= Math.sqrt(val); i++){
if(val % i == 0){
return false;
}
}
return true;
}
}


function isOdd( val ){
if( val % 2 == 1 ){
return true;
} else {
return false;
}
}

function isEven( val ){
if( val % 2 == 0 ){
return true;
} else {
return false;
}
}


function show(){
var group = document.f1.f_sGroup.value

num_opts = numlist.length;
msg = ""
for( i=0; i < num_opts; i++ ){
numlist.options.selected = false;

msg = msg + ( numlist.options.value ) + "\n";




if( group == "odd" ){
if( isOdd( numlist.options.value )){
numlist.options.selected = true;
}
}


if( group == "even" ){
if( isEven( numlist.options.value )){
numlist.options.selected = true;
}
}

if( group == "prime" ){
if( isPrime( numlist.options.value )){
numlist.options.selected = true;
}
}
}
//alert( msg )
}

-->
</script>






<table width = "70%" align="center">
<tr><td>

<form name="f1">
<select multiple name="f_sNums" size="10">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</td>


<td>
<select multiple name="f_sGroup" size="10" onchange="show()">
<option value="odd">Odd</option>
<option value="even">Even</option>
<option value="prime">Prime</option>
</select>
</form>



</td></td></table>

<script language="JavaScript">
<!--
/* Create aliases for form elements */
var numlist = document.f1.f_sNums;
var grouplist = document.f1.f_sGroup;
-->
</script>
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Thu, 15 Sep 2005 06:48:20, seen in (e-mail address removed) posted :
Obviously I read the "How to ask smart questions" FAQ _after_ I posted
my question. Sorry about that.

Now read the bit about not top-posting and not over-quoting. Compliance
encourages assistance.

<script language="JavaScript">

function isOdd( val ){
if( val % 2 == 1 ){
return true;
} else {
return false;
}
}

function isOdd(X) { return X%2==1 } // is clearer
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Fri, 16 Sep 2005 21:04:23, seen in Mick White
Dr John Stockton wrote:
[...]
function isOdd(X) { return X%2==1 } // is clearer

OR

function isOdd(X) { return X&1 }// returns 1 or 0

True; but a function called isOdd should IMHO return a genuine Boolean;
that's why I deleted X&1 and X%2 from my draft answer. One could put
!!(X%2) or !!(X&1).
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top