> I want to write a javascript function that I can use on all of my
> forms and inputs to disable a text input. So I want to pass to the
> function the form's ID and the text input's ID to be disabled.
After fixing the reference to the field as Adrian suggested you can genericize
this further.
Passing the "this" keyword to a handler will pass a reference to the element
registered with the handler: in this case the form element. Also the form
containing an element can be accessed FROM any element using the "form"
property.
So if you pass the element (instead of the form name) you can access everything
you need a bit more simply:
Function disabler(Trigger,InputID) {
var CurForm = Trigger.form;
CurForm[InputID].disabled=true;
};
<form id="foo">
<input type="text" id="moo">
<input type="button" onClick="disabler(this,'moo');">
</form>
Both the handler and the call become more generic (this will work on Forms
without IDs as well).
To further this train of thought you might also examine which fields you'd like
disabled. If you want all text fields in the form disabled it's simple to loop
over the form's "elements" array, checking for type = "text" and disabling them.
The following function is a framework. It takes an element of a form (using
the "this" keyword in the handler as above) and loops through all the elements
in it. The switch statement lets you do something different to each type of
form element.
Function disabler(Trigger) {
// Get a Reference to the Form
var CurForm = Trigger.form;
// Get a Reference to the elements array
var CurElements = CurForm.elements;
var CurField, CurType, CurName;
// Loop over the elements array
for (var Cnt = 0; Cnt < CurElements.length; Cnt++) {
// Get a Reference to the current element
CurField = CurFormElements[Cnt];
// Get some properties of the current element
CurType = CurField.type;
CurName = CurField.name;
// Take action based on the type of the element
switch (CurType) {
case 'button':
break;
case 'submit':
break;
case 'reset':
break;
case 'checkbox':
break;
case 'radio':
break;
case 'file':
break;
case 'text':
break;
case 'textarea':
break;
case 'hidden':
break;
case 'password':
break;
case 'select-one':
break;
case 'select-multiple':
break;
};
};
So if you insert "CurField.disabled=true;" into the "text" case it will
disabled all text elements in the form when the button is clicked.
You can obviously remove the "break" statements to chain multiple types to the
same action as well.
I'm sorry - I've not tested this code but I think it should work. If you have
any trouble please post back.
Hope this helps,
Jim Davis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Create Web Applications With ColdFusion MX7 & Flex 2.
Build powerful, scalable RIAs. Free Trial
http://www.adobe.com/products/coldfusion/flex2/?sdid=RVJS
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:278457
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4