Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically disabling form fields

Status
Not open for further replies.

WebRic

Technical User
Joined
Sep 21, 2004
Messages
95
Location
GB
Hi,

I've got a problem with the javascript below. I receive an error stating that there is an "Expected Identifier" on lin3 13 char 37 which is the line beginning var getWhichField.

Code:
//Disable Fields
function disableField (thisField, whichField)
	{
	var getThisField = document.form1. + thisField;
	var getWhichField = document.form1. + whichField;
	if (getThisField.value == '') {
			getWhichField.disabled = false;
			} else {
				if (getWhichField == '') {
				getWhichField.disabled = true;
				}else{
				document.alert("You should not add a value as you already are using the " + whichField + " Field")
				}
		}
	}

variables are to be passed to the script via a form field
Code:
onChange="disableField('redirect','webaddress')"

What am I doing wrong.

NB. Ideally I'd like to be able to pass any number of fields in the onChange event to disable multiple fields... is this possible?
Thanks,


Richard
 
Try this instead:
Code:
var getThisField = document.form1.elements[thisField];
var getWhichField = document.form1.elements[whichField];

And it's quite possible to pass an unspecified number of arguments to a function for processing. Just access them inside the function using the arguments array as you would any other array.

Lee
 
You can't dynamically build a reference like that. Use the forms and elements collections instead:

Code:
var getThisField = document.[!]forms[[/!]"form1"[!]].elements[[/!]thisField[!]][/!];
note: thisField was not encapsulated in quotes because it's a string being passed into that variable

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Yeah, you keep leaving me in the dust [lol]


Guess I'm too busy formatting my posts with the [ignore][!][/!][/ignore] tags.

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Thanks for the really quick and useful responses.


Richard
 
Hi again,

I've tried building an array into the script so that I can pass multiple fields but I think I've made a bit of a hash of it.

I'm not even sure if I've got the principals right? At the moment the syntax error is occuring in line 30 which is the line with the last else

Code:
//Disable Fields
function disableField (thisField, whichField)
	{
	var getThisField = document.forms["form1"].elements[thisField];
	if (getThisField.value == '') 
		{
		 for (i = 0;i < whichField;i++)
			{
			document.forms["form1"].elements[i].disabled = false;
			}
		} else {
				for (i = 0;i < whichField;i++)
					{
				if (document.forms["form1"].elements[i].value == '')
						{
						 for (i = 0;i < whichField;i++)
							{
							document.forms["form1"].elements[i].disabled = true;
							}
						}
					} else {
				document.alert("You should not add a value as you already are using the " + whichField + " Field(s)")
							}
				}
	}

With values being passed

Code:
onChange="disableField('redirect',new Array('webaddress','IPAddress'))"

Am I even close to getting it right?


Richard
 
Format your code so your braces line up, not like you have it now, and you'll be able to see the error easily.

Lee
 
Here's your code, formatted differently:
Code:
function disableField (thisField, whichField)
{
var getThisField = ....
if (getThisField.value == '') 
  {
  for (i = 0;i < whichField;i++)
    {
    document.forms["form1"]....;
    }
  }
else
  {
  for (i = 0;i < whichField;i++)
    {
    if (document.forms["form1"]...)
      {
      for (i = 0;i < whichField;i++)
        {
        document.forms...
        }
      }
    }
  else
    {
    alert(...)
    }
  }
}

Lee
 
Thanks Lee that does make things alot clearer.

I think I've nearly cracked it now. The syntax for the JS appears to be fine

Code:
<input name="redirect" type="text" id="redirect" value="" class="theInputBox" onChange="javascript:disableField('redirect',new Array('webaddress','IPAddress'))" />
 
<continued>
Click submit by accident.

The only problem I seem to be getting is passing the array in the onChange event.

Is this right?
 
Change this:

Code:
onChange="javascript:disableField('redirect',new Array('webaddress','IPAddress'))"

to this:

Code:
onChange="disableField('redirect', ['webaddress','IPAddress']);"

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Oh yes - and this:

Code:
document.forms["form1"].elements[i].disabled = true;

should be:

Code:
document.forms["form1"].elements[[!]whichField[i][/!]].disabled = true;

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
In fact, any reference to "elements" should be "elements[whichField]".

Put that altogether and see if it works... if not, post the new modified code with all those changes.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi All,

I've adjusted the code both parts to the code (i.e the onChange bit and the js file)

The onChange part is not kicking up an error any more but I am getting the synatax error on line 36 char 3. This is the last else in the statment
(NB. I've noticed that on previous errors on this file the line number has been 1 higher, so I'm assuming the same is still happening)

Here is the adjusted code

Code:
//Disable Field	
function disableField (thisField, whichField)
{
	var getThisField = document.forms["form1"].elements[thisField];
if (getThisField.value == '') 
  {
  for (var i=0; i<whichField.length; i++)
    {
	document.forms["form1"].elements[whichField[i]].disabled = false;
    }
  }
else
  {
  for (var i=0; i<whichField.length; i++)
    {
    if (document.forms["form1"].elements[whichField[i]].value == '')
      {
      for (i = 0;i < whichField;i++)
        {
		document.forms["form1"].elements[whichField[i]].disabled = true;
        }
      }
    }
  else
    {
    alert("You should not add a value as you already are using the  Field")
    }
  }
}

 
This is because your code is still incorrect. I think you should make better use of indents - when I tidied up yours, I spotted the error immediately:

Code:
//Disable Field    
function disableField (thisField, whichField) {
	var getThisField = document.forms["form1"].elements[thisField];
	if (getThisField.value == '') {
		for (var i=0; i<whichField.length; i++) {
			document.forms["form1"].elements[whichField[i]].disabled = false;
		}
	} else {
		for (var i=0; i<whichField.length; i++) {
			if (document.forms["form1"].elements[whichField[i]].value == '') {
				for (i = 0;i < whichField;i++) {
					document.forms["form1"].elements[whichField[i]].disabled = true;
				}
			}
		} else {
			alert("You should not add a value as you already are using the  Field")
		}
	}
}

You have an "else" statement coming after a "for" statement, which is wrong - they should come after "if" statements. Fix that, and you should be set.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks all. I've now got it working and am really pleased with the results.

I'll guess I'll have to start working on my formatting from now on.

Cheers,


Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top