Fetch the Web -- Homepage
Working together for your benefit

Javascript Cascading Form Values - part 2

A few weeks ago, I started a simple tutorial on how to cascade JavaScript check boxes from one row to the next in order to simplify the manipulation of large forms. This week, I’ll take it a step further by adding the same ability to both drop downs and text fields.

I’ve created a small example to help illustrate the functionality of the cascade. Once you load the cascade example page and click the down arrow next to a text field. It should change all the values in the text fields below to the same value as appears in the text box you clicked. The same can be done in the active column. Change one of the values in the active column to True and then click the down arrow next to it and all the rows below will change to True.

Without further discussion, here’s the Javascript code:

function cascSelect(theElement) {
var theForm = theElement.form;
var startBox = theElement.name;
for(z=0; z

if(theForm[z].type == ‘checkbox’ && theForm[z].name != ‘checkall’){
var checkNames = theForm[z].name;
if(checkNames == startBox.replace(/casca/, ‘checkeddelete’)) {
var startValue = (theForm[z].checked);
var startKey = z;
}
if (z > startKey) {
theForm[z].checked = startValue;
}
}
}
}
function cascScore(theElement) {
var theForm = theElement.form;
var startBox = theElement.name;
for(z=0; z
if(theForm[z].type == ‘text’){
var checkNames = theForm[z].name;
var tmp = startBox.replace(/cascScore/, ”);
tmp += ’score’;
if(checkNames == tmp) {
var startValue = (theForm[z].value);
var startKey = z;
}
if(theForm[z].name.match(/[0-9]score/)) {
if (z > startKey) {
theForm[z].value = startValue;
}
}
}
}
}
function selectTrueFalse(theElement) {
var theForm = theElement.form;
var startBox = theElement.name;
for(z=0; z
if(theForm[z].type == ’select-one’){
var checkNames = theForm[z].name;
var tmp = startBox.replace(/selectTrueFalse/, ”);
tmp += ‘active’;
if(checkNames == tmp) {
var startValue = (theForm[z].value);
var startKey = z;
}
if(theForm[z].name.match(/[0-9]active/)) {
if (z > startKey) {
theForm[z].value = startValue;
}
}
}
}
}
The only thing you have to watch for is the naming scheme. I was pretty lazy when I put all of this together and so it doesn’t follow any clean schema so to speak. Make sure the form fields have the same name as the JavaScript functions and you should be set.

Hope this helps some of you out, like I said before it may have saved me a few hours of work had I known it existed previously. It can be cleaned up quite a bit and I know all three functions could be lumped together into a more dynamic function if you like, but I’m not going to trouble with that here.

Cheers! Charles :-)

2 Responses to “Javascript Cascading Form Values - part 2”

  1. admin Says:

    Now that I’ve added the arrow image (down.gif), the example should be more understandable.

  2. Frank Says:

    I was confused by the submit and delete buttons. Now that I played with it and understand how it works its pretty nifty. Thanks!

Leave a Reply