By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

Adding up combinations with select-multiple boxes

P: n/a
Hi guys,

I'm trying to figure out what bone headed mistake I made on something I
put together. I've got a form (named 'context') that has a variable
number of select-multiple inputs on it. Based on the number of
variables passed through a GET string, I want to multiply the total
number of selected items for each together to see how many possible
combinations the selected items are generating.

The following snippet of code succesfully gets the values from the GET
statement to count the number of items being passed (I know, it assumes
the variable will always be in the same place, I'll deal with that
later), then it's supposed to loop through all elements in the page.
If the item it finds is a select-multiple input (up to this point, it
works. It finds each select-multiple and alerts in debugging), it's
supposed to do a loop inside that element to count how many are
checked, then multiply it against the running total (which is named
predictioncount). I expect the number for predictioncount to be 0
until the last field has an entry in it, but it never stops being
anything BUT zero.

I'm no Javascript expert, and my kludgy code shows it, but I think I'm
close to this functioning and I'm hoping someone here will see and say
"Oh, you got the standard whatsit confused with the thingy, just change
character x to y and it should work".

Thanks in advance for any assistance!

function trackselections()
{
//We'll get the variable passed, then get the name of it. If the item
is selected, we add one to the item. If not, we subtract it
//This lets us map out what will be submitted and calculate complexity
based on that.
var num = document.context.length;
var count=0;
var predictioncount = 0;
var testunit_count = 0;
//First, count how many testcase_ids are passed.
var getstring = location.search.substr(1);
var testunit_ids = 0;
count = getstring.search(/\&/);
getstring = getstring.substr(13,count-13);
testunit_ids = getstring.split(/%2C/g);
testunit_count = testunit_ids.length;

//Next, count all inputs of of the type select-multiple and loop
through them,
//collecting info on how many items are checked

for (var i = 0; i < num; i++)
{
if (document.context[i].type == 'select-multiple')
{
count = 0;
for (var o = 0; o< document.context[i].length; o++)
{
if (document.context[i].id[o].selected) count++;
alert(count);
}
predictioncount = predictioncount * count;
}
alert(predictioncount);
}

}

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Ben Hallert" <be*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi guys,

I'm trying to figure out what bone headed mistake I made on something I
put together. I've got a form (named 'context') that has a variable
number of select-multiple inputs on it. Based on the number of
variables passed through a GET string, I want to multiply the total
number of selected items for each together to see how many possible
combinations the selected items are generating.

The following snippet of code succesfully gets the values from the GET
statement to count the number of items being passed (I know, it assumes
the variable will always be in the same place, I'll deal with that
later), then it's supposed to loop through all elements in the page.
If the item it finds is a select-multiple input (up to this point, it
works. It finds each select-multiple and alerts in debugging), it's
supposed to do a loop inside that element to count how many are
checked, then multiply it against the running total (which is named
predictioncount). I expect the number for predictioncount to be 0
until the last field has an entry in it, but it never stops being
anything BUT zero.

I'm no Javascript expert, and my kludgy code shows it, but I think I'm
close to this functioning and I'm hoping someone here will see and say
"Oh, you got the standard whatsit confused with the thingy, just change
character x to y and it should work".

Thanks in advance for any assistance!

function trackselections()
{
//We'll get the variable passed, then get the name of it. If the item
is selected, we add one to the item. If not, we subtract it
//This lets us map out what will be submitted and calculate complexity
based on that.
var num = document.context.length;
var count=0;
var predictioncount = 0;
var testunit_count = 0;
//First, count how many testcase_ids are passed.
var getstring = location.search.substr(1);
var testunit_ids = 0;
count = getstring.search(/\&/);
getstring = getstring.substr(13,count-13);
testunit_ids = getstring.split(/%2C/g);
testunit_count = testunit_ids.length;

//Next, count all inputs of of the type select-multiple and loop
through them,
//collecting info on how many items are checked

for (var i = 0; i < num; i++)
{
if (document.context[i].type == 'select-multiple')
{
count = 0;
for (var o = 0; o< document.context[i].length; o++)
{
if (document.context[i].id[o].selected) count++;
alert(count);
}
predictioncount = predictioncount * count;
}
alert(predictioncount);
}

}


I think you want to replace
if (document.context[i].id[o].selected) count++;
with this
if (document.context[i].options[o].selected) count++;

I don't think you want this:
predictioncount = predictioncount * count;
don't you mean
predictioncount = predictioncount +count;
or better yet:
predictioncount += count;

Jul 23 '05 #2

P: n/a
Lee
Ben Hallert said:

It finds each select-multiple and alerts in debugging), it's
supposed to do a loop inside that element to count how many are
checked, then multiply it against the running total (which is named
predictioncount). I expect the number for predictioncount to be 0
until the last field has an entry in it, but it never stops being
anything BUT zero.


If you start with zero, and keep multiplying that value by any
other numbers, you will always end up with zero.

If I understand you correctly, you want to count the number of
selected items and THEN multiply that number by the number of arguments.

Jul 23 '05 #3

P: n/a
Ben Hallert wrote:
Hi guys,

I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable
number of select-multiple inputs on it. Based on the number of
variables passed through a GET string, I want to multiply the total
number of selected items for each together to see how many possible
combinations the selected items are generating.

The following snippet of code succesfully gets the values from the GET statement to count the number of items being passed (I know, it assumes the variable will always be in the same place, I'll deal with that
later), then it's supposed to loop through all elements in the page.
If the item it finds is a select-multiple input (up to this point, it
works. It finds each select-multiple and alerts in debugging), it's
supposed to do a loop inside that element to count how many are
checked, then multiply it against the running total (which is named
predictioncount). I expect the number for predictioncount to be 0
until the last field has an entry in it, but it never stops being
anything BUT zero.

I'm no Javascript expert, and my kludgy code shows it, but I think I'm close to this functioning and I'm hoping someone here will see and say "Oh, you got the standard whatsit confused with the thingy, just change character x to y and it should work".

Thanks in advance for any assistance!

function trackselections()
{
//We'll get the variable passed, then get the name of it. If the item is selected, we add one to the item. If not, we subtract it
//This lets us map out what will be submitted and calculate complexity based on that.
var num = document.context.length;
var count=0;
var predictioncount = 0;
var testunit_count = 0;
//First, count how many testcase_ids are passed.
var getstring = location.search.substr(1);
var testunit_ids = 0;
count = getstring.search(/\&/);
getstring = getstring.substr(13,count-13);
testunit_ids = getstring.split(/%2C/g);
testunit_count = testunit_ids.length;

//Next, count all inputs of of the type select-multiple and loop
through them,
//collecting info on how many items are checked

for (var i = 0; i < num; i++)
{
if (document.context[i].type == 'select-multiple')
{
count = 0;
for (var o = 0; o< document.context[i].length; o++)
{
if (document.context[i].id[o].selected) count++;
alert(count);
}
predictioncount = predictioncount * count;
}
alert(predictioncount);
}

}


I may be missing your point - but how can you read the submitted data
from the querystring until you've submitted the form? Why even bother,
as you can analyze it programmatically without performing any
particular action.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<script type="text/javascript">
//<![CDATA[

function getCombos()
{
var s = 0, sel, sels = document.getElementsByTagName('select'),
o, opt, opts,
nsel, predictioncount = 1;
while (sel = sels.item(s++))
{
if (/multiple/.test(sel.type))
{
opts = sel.getElementsByTagName('option');
o = 0;
nsel = 0;
while (opt = opts.item(o++))
if (opt.selected)
++nsel;
}
predictioncount *= nsel || 1;
}
return predictioncount;
}

//]]>
</script>
</head>
<body>
<form style="width:124px;">
<select size="5" multiple="multiple">
<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>
</select>
<select size="5" multiple="multiple">
<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>
<select size="5" multiple="multiple">
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
</select>
<hr />
<input type="button" value="get combos" onclick="alert(getCombos())" />
</form>
</body>
</html>

Jul 23 '05 #4

P: n/a
Good point on the starting from zero, I'll fix that immediately. I was
trying to do it all on the fly instead of storing my values in an array
because I felt I'd avoid some trouble, but that may not have been the
case.

Jul 23 '05 #5

P: n/a
The reason I was doing predictioncount = predictioncount * count is
because each time it counts up the selected items, I wanted it to
multiply the whole number by that. Eg, if I have 3 variables in the
GET input, and 4 mutliple-select fields that each have two selected,
then the total number of combinations (that I was trying to predict)
would be 3*2*2*2*2=48 total units created. If Iadd them the way you
showed, it would only be tallying the total number of selected items,
not showing the multiplied total.

Eureka! I applied your suggestion (changing id[o] to options[o]) and
changed my initial value for predictioncount to be testunit_ids.length
(to get the number of items passed in the GET variable string I was
looking at) and it works now! Thank you, McKirahan, RobB, and Lee for
your help, it got me over the little hump I was stuck at.
Regards,

Ben

Jul 23 '05 #6

P: n/a
As a followup, here's the working code. It's might not be as elegant
as RobB's, but it might be of use to some faceless denizen of the
future who stumbles into a similar problem while learning JS:

function trackselections()
{
//We'll get the variable passed, then get the name of it. If the item
is selected, we add one to the item. If not, we subtract it
//This lets us map out what will be submitted and calculate complexity
based on that.
var num = document.context.length;
var count=0;
var predictioncount;
var testunit_count = 0;
//First, count how many testcase_ids are passed.
var getstring = location.search.substr(1);
var testunit_ids = 0;
count = getstring.search(/\&/);
getstring = getstring.substr(13,count-13);
testunit_ids = getstring.split(/%2C/g);
predictioncount = testunit_ids.length;
//Next, count all inputs of of the type select-multiple and loop
through them,
//collecting info on how many items are checked

for (var i = 0; i < num; i++)
{
if (document.context[i].type == 'select-multiple')
{
count = 0;
for (var o = 0; o< document.context[i].length; o++)
{
if (document.context[i].options[o].selected) count++;
}
predictioncount = predictioncount * count;
}
}

//If the total number of test units being created is
//over 1000 or so, alert with a warning
if(predictioncount >=500 && predictioncount <=1000)
{
alert("If you submit this, you will create " + predictioncount + "
testunits. Consider making assignments in smaller groups.");
}
if(predictioncount >=1001 && predictioncount <=3000)
{
alert("If you submit this, you will create " + predictioncount + "
testunits. This may take a while (best case scenario) or crash your
browser (worst case scenario). Consider making assignments in smaller
groups.");
}
if(predictioncount >=3001)
{
alert("If you submit this, you will be asking the server to create "
+ predictioncount + " testunits. While the finely crafted swiss
worksmanship of the G5 that serves Toro is ready and willing for the
challenge, your browser most certainly isn't. You _will_ break your
user session, and to add insult to injury, the mind bending quantity of
work you've attempted to create will fall backwards into the shapeless
ether from which it came. Give a hoot, don't pollute.");
}

}

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.