function w/ parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • danny

    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('bizfo rm') or
    BizReset(getEle mentById('bizfo rm') statements I receive the error
    "elements.lengt h is either null or an object".
    Any Suggestions?

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


  • RobG

    #2
    Re: function w/ parameter

    danny wrote:[color=blue]
    > I have a function that when uses the form name works correctly. Should I
    > pass a variable to the function using either BizReset('bizfo rm') or[/color]

    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(curfor m) {
    var f = document.forms[curform];
    // now do things with f
    [color=blue]
    > BizReset(getEle mentById('bizfo rm') statements I receive the error[/color]

    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="BizRes et(getElementBy Id('bizform')); " ...
    ...

    function BizReset(curfor m) {
    // curform will be a reference to the form
    var len = curform.element s.length;
    ...
    [color=blue]
    > "elements.lengt h is either null or an object".
    > Any Suggestions?
    >
    > <script = "text/javascript>[/color]

    <script type="text/javascript">
    [color=blue]
    > function BizReset(curfor m) {
    > len = curform.element s.length;[/color]

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

    var len = curform.element s.length;
    [color=blue]
    > var blank = ""
    > for(i=0;i<len;i ++) {[/color]

    Same with 'i'

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

    [color=blue]
    > if (curform.elemen ts[i].type == "text") {
    > curform.element s[i].value = blank;[/color]

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

    curform.element s[i].value = '';
    [color=blue]
    > curform.element s[i].disabled=false ;
    > }
    > }
    > QuoteLayerOn(0) ;
    > DisplayButtons( 0);[/color]

    If these are not part of your problem, remove them for the sake
    if fixing your error.
    [color=blue]
    > document.curfor m.BizName.focus ();[/color]

    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.BizNam e.focus) curform.BizName .focus();
    [color=blue]
    > }
    > </script>
    >
    >[/color]

    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(curfo rm) {
    var len = curform.element s.length;
    for(var i=0;i<len;i++) {
    if (curform.elemen ts[i].type == "text") {
    curform.element s[i].value = '';
    curform.element s[i].disabled=false ;
    }
    }
    if (curform.BizNam e.focus) curform.BizName .focus();
    }

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

    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.focu s) f.BizName.focus ();
    }

    </script>
    <form action="" name="BizForm">
    <input type="text" name="BizName" value="somethin g" disabled>
    <input type="button" onclick="
    BizResetA(this. form);" value="this.for m">
    <input type="button" onclick="
    BizResetB('BizF orm');" value="'BizName '">
    <input type="reset">
    </form>


    --
    Rob

    Comment

    • Mick White

      #3
      Re: function w/ parameter

      danny wrote:
      [color=blue]
      > I have a function that when uses the form name works correctly. Should I
      > pass a variable to the function using either BizReset('bizfo rm') or
      > BizReset(getEle mentById('bizfo rm') statements I receive the error
      > "elements.lengt h is either null or an object".
      > Any Suggestions?
      >[/color]

      BizReset(docume nt.getElementBy Id('bizform'));
      or better:
      BizReset(docume nt.forms['bizform']);

      Mick

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

      Comment

      • danny

        #4
        Re: function w/ parameter

        Mick / Rob,

        Thanks. Excellent explanations. Everything is working correctly.

        "danny" <dthomas@plan et-inc.net> wrote in message
        news:112gf086kk 14571@corp.supe rnews.com...[color=blue]
        > I have a function that when uses the form name works correctly. Should I
        > pass a variable to the function using either BizReset('bizfo rm') or
        > BizReset(getEle mentById('bizfo rm') statements I receive the error
        > "elements.lengt h is either null or an object".
        > Any Suggestions?
        >
        > <script = "text/javascript>
        > function BizReset(curfor m) {
        > len = curform.element s.length;
        > var blank = ""
        > for(i=0;i<len;i ++) {
        > if (curform.elemen ts[i].type == "text") {
        > curform.element s[i].value = blank;
        > curform.element s[i].disabled=false ;
        > }
        > }
        > QuoteLayerOn(0) ;
        > DisplayButtons( 0);
        > document.curfor m.BizName.focus ();
        > }
        > </script>
        >
        >[/color]


        Comment

        Working...