473,566 Members | 2,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Select boxes to select sets

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.

Sep 14 '05 #1
5 2379
ca********@gmai l.com wrote:
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.c o.uk/group/comp.lang.javas cript/browse_frm/thread/4aee4ddbd52c97c 4/79d89c8dd0f2e16 9?q=dynamic+opt ion+list&rnum=5 &hl=en#79d89c8d d0f2e169>

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="showNu mObj();"><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.


--
Rob
Sep 14 '05 #2
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="JavaS cript">
<!--

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_s Group.value

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

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


if( group == "odd" ){
if( isOdd( numlist.options[i].value )){
numlist.options[i].selected = true;
}
}
if( group == "even" ){
if( isEven( numlist.options[i].value )){
numlist.options[i].selected = true;
}
}

if( group == "prime" ){
if( isPrime( numlist.options[i].value )){
numlist.options[i].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">Ev en</option>
<option value="prime">P rime</option>
</select>
</form>

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

<script language="JavaS cript">
<!--
/* Create aliases for form elements */
var numlist = document.f1.f_s Nums;
var grouplist = document.f1.f_s Group;
-->
</script>
-------------------------------------------

RobG wrote:
ca********@gmai l.com wrote:
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.c o.uk/group/comp.lang.javas cript/browse_frm/thread/4aee4ddbd52c97c 4/79d89c8dd0f2e16 9?q=dynamic+opt ion+list&rnum=5 &hl=en#79d89c8d d0f2e169>

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="showNu mObj();"><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.


--
Rob


Sep 15 '05 #3
JRS: In article <11************ *********@g47g2 000cwa.googlegr oups.com>,
dated Thu, 15 Sep 2005 06:48:20, seen in news:comp.lang. javascript,
ca********@gmai l.com 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="JavaS cript">
Deprecated : <script type="text/javascript">

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


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

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 16 '05 #4
Dr John Stockton wrote:
[...]

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

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


OR

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

Mick
Sep 16 '05 #5
JRS: In article <rr************ *******@twister .nyroc.rr.com>, dated
Fri, 16 Sep 2005 21:04:23, seen in news:comp.lang. javascript, Mick White
<mw***********@ rochester.rr.co m> posted :
Dr John Stockton wrote:
[...]
function isOdd( val ){
if( val % 2 == 1 ){
return true;
} else {
return false;
}
}

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).

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 17 '05 #6

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

Similar topics

12
8865
by: Forti2ude | last post by:
Hello, I have a simple form... <form> <select name="foo" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select>
3
2446
by: Adam Toline | last post by:
In reference to the following: http://www.bellecose.com/form.htm At the top of each column there is a box for "All". When one is checked I need to check all of (and only) those boxes underneath. Now, the rub here is that every checkbox on the page (except the "All"s)
4
2132
by: Jay | last post by:
I'm trying to build a pretty complex form that submits to an email, but am having problems with a certain bunch of funcionality in it... Have a look at this example page... http://www.exit7.co.uk/drop/yay/blankform.html I need to get the form to... (1) when the user selects an option from 'Select Box A' it populates the 4 blank select...
5
2123
by: Allan M. | last post by:
I have a series of select boxes that must be populated client side, because they interact with each other. The design specification calls for these boxes to be updated without having to make a roundtrip to the server. The codebehind seems to be unaware of select box members populated via javascript. So, I'm having to create my own state...
5
26266
by: Giggle Girl | last post by:
Is there away to change the border style of the "dropdown box" (Select input elements) for IE 6? By border style, I mean the initial light blue outline of a select box. If have successfully changes the border of its options, but the initial border is always light blue in IE6 running WIndows XP. >From reading here I believe there is no way...
5
3685
by: jjyconsulting | last post by:
Newbie needing some help. I have a tblParticipants. The fields include gender, education_level, income, occupation etc., I'm trying to create a form where a user can run a query from the form and just choose the appropriate criterias from the combo boxes to get the results. I also want the query to run even if there is not a value in all...
2
8414
by: simon.wilkinson | last post by:
Hi, I am trying to update all Select boxes on a page dynamically using javascript, I simple want to change the selected item in each select box when a tick box is pressed on the page. Each Select box is named in the same convention ie. ddl_DeliveryStatus_ and then the recordID and contains the same options in the same order. The number of...
1
4014
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i...
10
1754
by: brendanmcdonagh | last post by:
Hi all, I'm just getting to grips with java and have set my self a challenge to consolidate my learnings so far as well as implement new learnings. I want to enter a load of possible things to do into sets and then randomly generate a set value, which is a suggestion of what to do(Geeky - know!) There will be numerous sets, a set for me...
0
7584
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...
0
8108
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
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...
0
7951
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...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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...
0
3643
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...
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.