f



function w/ parameter

I have a function that when uses the form name works correctly. Should I
pass a variable to the function using either BizReset('bizform') or
BizReset(getElementById('bizform') statements I receive the error
"elements.length is either null or an object".
Any Suggestions?

<script = "text/javascript>
function BizReset(curform) {
    len = curform.elements.length;
 var blank = ""
 for(i=0;i<len;i++) {
   if (curform.elements[i].type == "text") {
     curform.elements[i].value = blank;
     curform.elements[i].disabled=false;
   }
 }
   QuoteLayerOn(0);
   DisplayButtons(0);
   document.curform.BizName.focus();
}
</script>


0
dthomas (7)
3/4/2005 10:43:38 AM
comp.lang.javascript 38286 articles. 0 followers. javascript4 (1315) is leader. Post Follow

3 Replies
334 Views

Similar Articles

[PageSpeed] 38

danny wrote:
> I have a function that when uses the form name works correctly. Should I
> pass a variable to the function using either BizReset('bizform') or

  Please hang in there, there's quite a bit to talk about here...

  This method of calling your function is incompatible with the
  code you have posted.  If you pass the name of the form as a
  string (as above), use:

  function BizReset(curform) {
    var f = document.forms[curform];
    // now do things with f

> BizReset(getElementById('bizform') statements I receive the error

If you are going to reference your form this way, you must put an 
id on the form 'bizform' and fix the syntax error.

   <form id="bizform" ... >
   ...

   ... onclick="BizReset(getElementById('bizform'));" ...
   ...

   function BizReset(curform) {
     // curform will be a reference to the form
     var len = curform.elements.length;
     ...

> "elements.length is either null or an object".
> Any Suggestions?
> 
> <script = "text/javascript>

   <script type="text/javascript">

> function BizReset(curform) {
>     len = curform.elements.length;

  Unless 'len' needs to be global, keep it local:

       var len = curform.elements.length;

>  var blank = ""
>  for(i=0;i<len;i++) {

  Same with 'i'

    for(var i=0; i<len; i++) {


>    if (curform.elements[i].type == "text") {
>      curform.elements[i].value = blank;

  The variable 'blank' is not needed, you could just write:

        curform.elements[i].value = '';

>      curform.elements[i].disabled=false;
>    }
>  }
>    QuoteLayerOn(0);
>    DisplayButtons(0);

  If these are not part of your problem, remove them for the sake
  if fixing your error.

>    document.curform.BizName.focus();

  You should check that the focus method is supported first (e.g.
  older versions of Safari don't).  And curform is a reference to
  the form, so 'document.' will cause an error:

  if (curform.BizName.focus) curform.BizName.focus();

> }
> </script>
> 
> 

  Here is a modified version of your form that shows different ways
  to do what I think you are trying to do.

<script type="text/javascript">
// Pass a reference to the form
function BizResetA(curform) {
  var len = curform.elements.length;
  for(var i=0;i<len;i++) {
    if (curform.elements[i].type == "text") {
      curform.elements[i].value = '';
      curform.elements[i].disabled=false;
    }
  }
  if (curform.BizName.focus) curform.BizName.focus();
}

// Pass the name of the form as a string
function BizResetB(curform) {
  var f = document.forms[curform];
  var len = f.elements.length;

  for(var i=0;i<len;i++) {
    if (f.elements[i].type == "text") {
      f.elements[i].value    = '';
      f.elements[i].disabled = false;
    }
  }
  if (f.BizName.focus) f.BizName.focus();
}

</script>
<form action="" name="BizForm">
   <input type="text" name="BizName" value="something" disabled>
   <input type="button" onclick="
     BizResetA(this.form);" value="this.form">
   <input type="button" onclick="
     BizResetB('BizForm');" value="'BizName'">
   <input type="reset">
</form>


-- 
Rob
0
RobG
3/4/2005 2:05:36 PM
danny wrote:

> I have a function that when uses the form name works correctly. Should I
> pass a variable to the function using either BizReset('bizform') or
> BizReset(getElementById('bizform') statements I receive the error
> "elements.length is either null or an object".
> Any Suggestions?
> 

BizReset(document.getElementById('bizform'));
or better:
BizReset(document.forms['bizform']);

Mick


> <script = "text/javascript>
> function BizReset(curform) {
>     len = curform.elements.length;
>  var blank = ""
>  for(i=0;i<len;i++) {
>    if (curform.elements[i].type == "text") {
>      curform.elements[i].value = blank;
>      curform.elements[i].disabled=false;
>    }
>  }
>    QuoteLayerOn(0);
>    DisplayButtons(0);
>    document.curform.BizName.focus();
> }
> </script>
> 
> 
0
Mick
3/4/2005 3:42:19 PM
Mick / Rob,

Thanks.  Excellent explanations. Everything is working correctly.

"danny" <dthomas@planet-inc.net> wrote in message
news:112gf086kk14571@corp.supernews.com...
> I have a function that when uses the form name works correctly. Should I
> pass a variable to the function using either BizReset('bizform') or
> BizReset(getElementById('bizform') statements I receive the error
> "elements.length is either null or an object".
> Any Suggestions?
>
> <script = "text/javascript>
> function BizReset(curform) {
>     len = curform.elements.length;
>  var blank = ""
>  for(i=0;i<len;i++) {
>    if (curform.elements[i].type == "text") {
>      curform.elements[i].value = blank;
>      curform.elements[i].disabled=false;
>    }
>  }
>    QuoteLayerOn(0);
>    DisplayButtons(0);
>    document.curform.BizName.focus();
> }
> </script>
>
>


0
danny
3/4/2005 5:28:18 PM
Reply: