B
Ben Hallert
Hi there,
I haven't done a lot of Javascript programming, so I'm not having great
luck debugging something I put together, and I was hoping someone here
could give it a quick lookover and maybe see the obvious mistake I'm
making.
My challenge is, I've been asked to make a change to an internal tool.
It has a screen with a tree control and a series of buttons at the top.
The buttons at the top control which form is submitted, and are
enabled/disabled based on what type of things are selected in the tree
below.
It currently uses radio buttons, but I've changed the items that are
parents to be checkboxes instead so I can have multiselect. To
maintain compatibillity with the other forms that expect a single
variable (named testclass_id) to be posted, I've made the following
action to be executed on submit:
function dealWithMultiSelect ()
{
/*This function should parse through all tcl set checkboxes to see if
there are more then one
//selected. If so, it needs to create a new hidden field with the
same name for each so we can
//send multiple instances of testclass_id to fulfillments review.
//Suggested flow, go through all of the checkboxes and see if there
are more then one active.
//In a sub loop, for each found, add a new hidden field.
//We'll modify set_val to make sure that there isn't a combination of
tcs and tcls set*/
num = document.tc.id.length;
for (var i = 0; i < num; i++)
{
if (document.tc.id.checked == true && document.tc.id.type ==
"checkbox")
{
//if it's not the FIRST tcl selected, then create a new hidden
element named testclass_id
if (TotalTCLSelected)
{
var hidden = document.createElement('input');
hidden.setAttribute('type','hidden');
hidden.setAttribute('value',document.tc.id.value);
hidden.setAttribute('name','testclass_id');
document.getElementById(document.appendChild(hidden));
}
TotalTCLSelected++;
}
}
}
As you can see, I'm trying to use createelement to add more hidden
variables of the same name (in this case, 'testclass_id') so that I'll
be emulating a multi-select GET on the next screen. The intention is
that it loops through all the inputs in the form and, if the item is
checked AND is of the type 'checkbox', it adds a new hidden input with
the value of that item. End result, it should submit multiple
instances of 'testclass_id'. I increment TotalTCLSelected so that it
doesn't start doing that until the second one it encounters on purpose,
that's also to maintain compatibillity with other forms.
Can anyone offer some suggestions? My main theory is that the problem
has to do with one or more of the following:
1. Can I really keep redefining the var hidden in the loop? And does
it get cleared each time?
2. Am I messing up the object inheritance by doing
document.createelement instead of something like
document.tc.createelement? (where tc is the name of the form being
parsed)
3. I didn't define TotalTCLSelected anywhere. It isn't erroring out,
but could this be silently killing it?
4. Other?
Thanks!
I haven't done a lot of Javascript programming, so I'm not having great
luck debugging something I put together, and I was hoping someone here
could give it a quick lookover and maybe see the obvious mistake I'm
making.
My challenge is, I've been asked to make a change to an internal tool.
It has a screen with a tree control and a series of buttons at the top.
The buttons at the top control which form is submitted, and are
enabled/disabled based on what type of things are selected in the tree
below.
It currently uses radio buttons, but I've changed the items that are
parents to be checkboxes instead so I can have multiselect. To
maintain compatibillity with the other forms that expect a single
variable (named testclass_id) to be posted, I've made the following
action to be executed on submit:
function dealWithMultiSelect ()
{
/*This function should parse through all tcl set checkboxes to see if
there are more then one
//selected. If so, it needs to create a new hidden field with the
same name for each so we can
//send multiple instances of testclass_id to fulfillments review.
//Suggested flow, go through all of the checkboxes and see if there
are more then one active.
//In a sub loop, for each found, add a new hidden field.
//We'll modify set_val to make sure that there isn't a combination of
tcs and tcls set*/
num = document.tc.id.length;
for (var i = 0; i < num; i++)
{
if (document.tc.id.checked == true && document.tc.id.type ==
"checkbox")
{
//if it's not the FIRST tcl selected, then create a new hidden
element named testclass_id
if (TotalTCLSelected)
{
var hidden = document.createElement('input');
hidden.setAttribute('type','hidden');
hidden.setAttribute('value',document.tc.id.value);
hidden.setAttribute('name','testclass_id');
document.getElementById(document.appendChild(hidden));
}
TotalTCLSelected++;
}
}
}
As you can see, I'm trying to use createelement to add more hidden
variables of the same name (in this case, 'testclass_id') so that I'll
be emulating a multi-select GET on the next screen. The intention is
that it loops through all the inputs in the form and, if the item is
checked AND is of the type 'checkbox', it adds a new hidden input with
the value of that item. End result, it should submit multiple
instances of 'testclass_id'. I increment TotalTCLSelected so that it
doesn't start doing that until the second one it encounters on purpose,
that's also to maintain compatibillity with other forms.
Can anyone offer some suggestions? My main theory is that the problem
has to do with one or more of the following:
1. Can I really keep redefining the var hidden in the loop? And does
it get cleared each time?
2. Am I messing up the object inheritance by doing
document.createelement instead of something like
document.tc.createelement? (where tc is the name of the form being
parsed)
3. I didn't define TotalTCLSelected anywhere. It isn't erroring out,
but could this be silently killing it?
4. Other?
Thanks!