Forum Moderators: open

Message Too Old, No Replies

What can I do with this

1500+ lines of JavaScript

         

grandpa

7:34 am on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK, I got something working over here but it sure is bulky. We're picking 4 items from a page that has 315 selections. Each option is defined in my form like this:

<form method="post" name="checkboxform">
.
.
<input type="checkbox" name="VAR3" value="1" onClick="countChoices(this)">
<input type="hidden" name="AddItem3" value="VAR3># 3
.
<!-- 315 items defined here -->
</form>

My function countChoices(obj) seems too kludgy - and I'm hoping someone can help me with a tune-up.

The function contains all 315 checkbox definitions:
box3 = obj.form.VAR3.checked;

then it performs a count:
count=(box3? 1 : 0)+(box4? 1 : 0).. +(box315? 1 : 0);

If the count exceeds my max (set at 4) then I call a function to clear all the checkboxes. That part works nicely.

Can I define all these checkboxes with an array and also use array counting to determine the number of items selected? It seems to me that array processing might be quicker than reading 1500 lines of code every time I click a checkbox.

MonkeeSage

10:21 am on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You got it. It should be alot quicker to use an array of checkbox elements you can iterate through, and alot easier on the fingers than all that typing. ;)

Give this a whirl...

// max. simultaneously checked 
var checkedlimit = 4;

// array to hold all checkbox elements
var checks = new Array();

// function to fill checkbox elements array
function getCheckboxes() {
var count = 0;
var checkstmp = document.getElementsByTagName("input");
for (var j = 0; j < checkstmp.length; ++j) {
if (checkstmp[j].type == "checkbox") {
checks[count] = checkstmp[j];
++count;
}
}
}

// function to enforce checked limit
function checkCheckboxes() {
var checkcount = 0;
for (var j = 0; j < checks.length; ++j) {
if (checkcount <= checkedlimit) {
if (checks[j].checked) {
++checkcount;
}
}
else {
alert("You may only select " + checkedlimit +
" items at a time!");
return false;
}
}
return true;
}

// fill checkboxes array on page loaded
window.onload = getCheckboxes;

...

<div>
<form>
<input type="checkbox" onclick="return checkCheckboxes();" />
<input type="checkbox" onclick="return checkCheckboxes();" />
<input type="checkbox" onclick="return checkCheckboxes();" />
<input type="checkbox" onclick="return checkCheckboxes();" />
<input type="checkbox" onclick="return checkCheckboxes();" />
</form>
</div>

Nb. Make sure you get the

return
portion in there for the onclick values, that way if the validation function returns false (meaning more than checkedlimit were checked), it won't actually check any others because the
return false;
from the checkCheckboxes() function will get passed on as the return value of the onclick event, which will cancel it.

Jordan

grandpa

11:10 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you MonkeeSage, that is much faster. Of course now I want more from the page....but I can rock along for a while as is.

Last week I all about learning CSS, now I guess I need to change gears and look at JS. There's tons of scripts out there, but nothing beats knowing what you're doing. I'll be glad when we're big enough to move all this to an AS/400, something I know a little about.

coopster

11:30 pm on Sep 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



grandpa, you're writing javascript on your AS/400? ;) Just kidding, I'm an AS/400 pro too. However, picking up JS is essential if you're doing any web development (or intranet development) whatsoever. Don't do what I did and try to pick JS up here and there. I bought the JavaScript Bible by Danny Goodman and it was well worth the coin. Just some friendly advice since we aren't all as fluent as MonkeeSage here (MonkeeSage, that's a compliment :)). Best of luck, grandpa.

grandpa

3:57 am on Sep 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Doesn't everyone use JS on their 400's? :)

I've been messing with this thing for a few hours now, and I'm about ready to go back to the drawing board. For some reason I get a few "extra" selections sent along with the actual choices, so 5 or 6 or more items might end up on my cart. That's not exactly what I had in mind. Seems like I glanced at something yesterday that mentioned that problem with radio buttons and checkboxes? It also only seems to occur if the max limit is exceeded. I"m resetting all the checkboxes to false when this happens, so I'm at a loss as to where the random extras are coming from.

In full "back up and punt mode" I'm thinking what I really want to do is have a drop down menu with all my selections. When one selection is made, click a button to add it to a list on my page. When the requisite 4 items are selected, no more and no less, then a second submit button will send the goods off to my cart. Under the covers I'm going to try to take those 4 variables and pass them into my cart string as a single value.

Thanks for the feedback. Looks like I'll be cobbling all night now :)

MonkeeSage

4:27 am on Sep 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm thinking what I really want to do is have a drop down menu with all my selections. When one selection is made, click a button to add it to a list on my page. When the requisite 4 items are selected, no more and no less, then a second submit button will send the goods off to my cart.

Sounds like a plan. :)

Will probably be much easier on the user that way as well since all of the selections will be centralized in the dropdown. You might play with the disabled attribute on the second (submit) button that goes to the cart. E.g.,

<input id="sub" type="submit" value="Checkout" disabled="true" />

Then once the correct number of selections is made, do...

document.getElementById("sub").disabled = false;

That way they only get the option to submit once they have made the requisite selections.

Jordan

Ps. Let us know if you need any other help, as JavaScript can sometimes bite you in the pants for no apparent reason, mind you...I've got the bite marks to prove it! ;)

grandpa

2:41 am on Sep 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well look who's back.

My redesign went pretty well for an amateur. But I have a question.

I can select an item from my drop down box, and I can populate a text field with the selection. My array TArray is defined globally so I can populate it from one form and get the results into second form. Nothing fancy here, trust me. But it works.

Here's my question. In my form "Add" I'm selectiong the options and calling function testSelect to get the variable name. What is getting returned is the right side of the option:
<option value="ALM">Almond</option>

I assume that's the .selectedIndex part of my Item definition. What can I do to also retreive the left part of my option, or "ALM"?

function testSelect (form) {
Item = Add.VARname.selectedIndex;
Result = Add.VARname.options[Item].text;
//alert (Result);
T = Add.VARname.options[Item].text;
fillform(T)
}

// don't cringe... it works.
function fillform(form) {
if (Add.T1.value == "")
{if (i == 1) Add.T1.value = T; TArray[0] = T; ++i;}
else if (Add.T2.value == "")
{if (i == 2) Add.T2.value = T; TArray[1] = T; ++i;}
else if (Add.T3.value == "")
{if (i == 3) Add.T3.value = T; TArray[2] = T; ++i;}
else if (Add.T4.value == "")
{if (i == 4) Add.T4.value = T; TArray[3] = T; ++i; document.getElementById("AddSubmit").disabled = false;}
}

grandpa

3:19 am on Sep 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here I am about to answer my own question. I guess this post won't count...

I added this line to my function testSelect:

Result2 = Add.VARname.options[Item].value;

Are there perhaps other options besides "text" and "value" that I can use here? Anyway, it's starting to make sense.

grandpa

2:13 am on Sep 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Folks, I got my project of the week completed yesterday... or was it this morning. And it's even slicker than I envisioned. Using a drop down box and a submit button in one form, loading an array, and populating a second form - it gets directed to my cart. Well, it's new to me :-)

I even ended up building my functions not from borrowed scripts but by looking at scripts and finding what I could on the internet. That's not to say I didn't use some borrowed script too... Yes, I need a JS Bible over here.

I believe what helped as much as anything was the ability to have a running dialog with the issues I was facing. The situation over here is that I'm speaking greek to anyone willing to listen, and it's greek I can't even speak fluently. So having somewhere to turn is a blessing.

I'd love to post the addy of my work, but rules are rules. I'm a proud grandpa today.

Thanks

MonkeeSage

5:11 am on Sep 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



grandpa:

Congrats! Sounds like you put alot of work into it, so enjoy the fruits! "By the sweat of your brow you shall eat bread." ;)

Two resources I couldn't do without for JavaScript are the Guide and Reference listed here (which can be viewed online or downloaded as zip packages):

[devedge.netscape.com...]

They cover everything -- and I do mean everything, and they usually give working examples to boot. :)

Jordan