function in a variable

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

    #1

    function in a variable

    hi all,


    I'm writing a function that, say, loop through input fields in a form
    and set default values to it. Some fields are dependent thus I have
    some onChange scripts to kick off corresponding actions to those
    fields.

    <form name="test">
    <input type="text" name="text1">
    <input type="text" name="text2" onChange="text1 .value+=this.va lue;">
    <input type="text" name="text3" onChange="doSom ething();">
    </form>

    <script language="JavaS cript">
    function setDefault(form ) {
    var fields = form.elements;
    for (var i=0; i<fields.length ; i++) {
    // for each field, somehow get a default value and set it.
    }
    }
    </script>
    ......

    [OnChange] catches only browser actions as far as I understand. That
    means when I set a value to "text2" via the method, the corresponding
    action isn't triggered.

    I can get the actions of a corresponding field by:
    var actn = field.onchange;
    alert( actn );

    which shows:
    anonymous() {
    doSomething();
    }


    Despite being able to get all those actions and store them in a
    variable, I don't know how to execute them. Do anyone has a clue?


    Thanks very much
    George
  • lallous

    #2
    Re: function in a variable

    Hello

    "xxgeorge" <xxgeorge@yahoo .com> wrote in message
    news:e9a5fab2.0 408102328.3ad84 4ed@posting.goo gle.com...[color=blue]
    > hi all,
    >
    >
    > I'm writing a function that, say, loop through input fields in a form
    > and set default values to it. Some fields are dependent thus I have
    > some onChange scripts to kick off corresponding actions to those
    > fields.
    >
    > <form name="test">
    > <input type="text" name="text1">
    > <input type="text" name="text2" onChange="text1 .value+=this.va lue;">
    > <input type="text" name="text3" onChange="doSom ething();">
    > </form>
    >
    > <script language="JavaS cript">
    > function setDefault(form ) {
    > var fields = form.elements;
    > for (var i=0; i<fields.length ; i++) {
    > // for each field, somehow get a default value and set it.
    > }
    > }
    > </script>
    > .....
    >
    > [OnChange] catches only browser actions as far as I understand. That
    > means when I set a value to "text2" via the method, the corresponding
    > action isn't triggered.
    >
    > I can get the actions of a corresponding field by:
    > var actn = field.onchange;
    > alert( actn );
    >
    > which shows:
    > anonymous() {
    > doSomething();
    > }
    >
    >
    > Despite being able to get all those actions and store them in a
    > variable, I don't know how to execute them. Do anyone has a clue?
    >[/color]
    Hmm...

    You wanted to execute a given action?

    Why not call it directly w/o storing it in a variable as:
    field.onChange( field); // <-- sending it the 'this' or itself
    or
    field.onChange( );

    HTH,
    Elias


    Comment

    • Michael Winter

      #3
      Re: function in a variable

      On Wed, 11 Aug 2004 11:05:04 +0300, lallous <lallous@lgwm.o rg> wrote:

      [snip]
      [color=blue]
      > Why not call it directly w/o storing it in a variable as:
      > field.onChange( field); // <-- sending it the 'this' or itself
      > or
      > field.onChange( );[/color]

      Remember that the properties are case-sensitive, unlike HTML. It should be:

      field.onchange( );

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail

      Comment

      • xxgeorge

        #4
        Re: function in a variable

        "lallous" <lallous@lgwm.o rg> wrote in message news:<2nu2ieF4n p6aU1@uni-berlin.de>...[color=blue]
        > Hello
        >
        > Hmm...
        >
        > You wanted to execute a given action?
        >
        > Why not call it directly w/o storing it in a variable as:
        > field.onChange( field); // <-- sending it the 'this' or itself
        > or
        > field.onChange( );
        >
        > HTH,
        > Elias[/color]


        oh thx, that do the trick. how I can miss this :)
        Anyway, just for interest, still want to know if there's any way to
        invoke a method obj. I mean something like "mthd.call( )".

        One more question. is there any way to list out all fields and
        methods of an object? say I have the "mthd" object, how can I know
        its attributes?


        many thx
        xxgeorge

        Comment

        • lallous

          #5
          Re: function in a variable

          "xxgeorge" <xxgeorge@yahoo .com> wrote in message
          news:e9a5fab2.0 408111747.13c9c fb@posting.goog le.com...[color=blue]
          > "lallous" <lallous@lgwm.o rg> wrote in message[/color]
          news:<2nu2ieF4n p6aU1@uni-berlin.de>...[color=blue][color=green]
          > > Hello
          > >
          > > Hmm...
          > >
          > > You wanted to execute a given action?
          > >
          > > Why not call it directly w/o storing it in a variable as:
          > > field.onChange( field); // <-- sending it the 'this' or itself
          > > or
          > > field.onChange( );
          > >
          > > HTH,
          > > Elias[/color]
          >
          >
          > oh thx, that do the trick. how I can miss this :)
          > Anyway, just for interest, still want to know if there's any way to
          > invoke a method obj. I mean something like "mthd.call( )".
          >
          > One more question. is there any way to list out all fields and
          > methods of an object? say I have the "mthd" object, how can I know
          > its attributes?
          >[/color]

          <script language="JavaS cript">
          <!--
          for (x in document)
          document.write( x + "<br>");
          //-->
          </script>

          --
          Elias


          Comment

          • Robert

            #6
            Re: function in a variable

            >[color=blue]
            > One more question. is there any way to list out all fields and
            > methods of an object? say I have the "mthd" object, how can I know
            > its attributes?[/color]

            Firefox has an object viewer.

            Mozilla’s family of products are all designed to keep you safer and smarter online




            You can loop through all the first level members of an object with:

            for (i in obj)
            {


            }


            You would have to put in check for nested objects etc.

            Robert

            Comment

            • Steve van Dongen

              #7
              Re: function in a variable

              xxgeorge@yahoo. com (xxgeorge) wrote:
              [color=blue]
              >"lallous" <lallous@lgwm.o rg> wrote in message news:<2nu2ieF4n p6aU1@uni-berlin.de>...[color=green]
              >> Hello
              >>[/color]
              >Anyway, just for interest, still want to know if there's any way to
              >invoke a method obj. I mean something like "mthd.call( )".[/color]

              function f() { alert('hello') }
              mthd = f;
              mthd();

              Regards,
              Steve

              Comment

              • xxgeorge

                #8
                Re: function in a variable

                hi all,


                thx for all your response. I now have another question. I'm trying
                to write a similar piece of script that automatically turns all text
                field data into uppercase. Here's the script I put at the bottom of
                my page:

                <script language="JavaS cript">
                var form = window.document .myForm;
                var fields = form.elements;
                for (var i=0; i<fields.length ; i++) {
                var field = fields[i];
                if (field.type.toU ppercase().inde xOf("TEXT")>=0) {
                field.onblur = toUpperCase;
                }
                }
                function toUpperCase() {
                var field = window.event.sr cElement;
                field.value = field.value.toU pperCase();
                }
                </script>

                It works fine but the question is, what if the field originally has a
                onBlur function? how can I tell it to do both it's own function and
                mine?


                Thanks for your help
                george

                Comment

                • Michael Winter

                  #9
                  Re: function in a variable

                  On 19 Aug 2004 21:30:57 -0700, xxgeorge <xxgeorge@yahoo .com> wrote:
                  [color=blue]
                  > thx for all your response. I now have another question. I'm trying
                  > to write a similar piece of script that automatically turns all text
                  > field data into uppercase. Here's the script I put at the bottom of
                  > my page:
                  >
                  > <script language="JavaS cript">[/color]

                  Valid HTML requires the use of the type attribute. The language attribute
                  has been deprecated and should not be used.

                  <script type="text/javascript">
                  [color=blue]
                  > var form = window.document .myForm;[/color]

                  No need to use window.
                  [color=blue]
                  > var fields = form.elements;
                  > for (var i=0; i<fields.length ; i++) {
                  > var field = fields[i];
                  > if (field.type.toU ppercase().inde xOf("TEXT")>=0) {[/color]

                  The type property returns a lowercase string. There is no reason to
                  convert it to uppercase. Also, if the field is an INPUT of type text, only
                  'text' will be returned.

                  if(field.type == 'text') {
                  [color=blue]
                  > field.onblur = toUpperCase;
                  > }
                  > }
                  > function toUpperCase() {
                  > var field = window.event.sr cElement;
                  > field.value = field.value.toU pperCase();[/color]

                  You should be able to change that to

                  this.value = this.value.toUp perCase();

                  as the this operator will reference the object that is handling the event
                  (the text field).
                  [color=blue]
                  > }
                  > </script>
                  >
                  > It works fine but the question is, what if the field originally has a
                  > onBlur function? how can I tell it to do both it's own function and
                  > mine?[/color]

                  It depends on your target environment. If this is to be used on an
                  Intranet, the browsers available will offer different approaches. For
                  example, IE allows

                  obj.attachEvent (type, listener) [1]

                  whilst DOM-compliant browsers offer

                  obj.addEventLis tener(type, listener, capture) [2]

                  However, if this is for the Web, you'll need a much more intricate method.
                  So, restricted environment, or the Web?

                  Mike


                  [1] The type parameter is the event name with the 'on' prefix.
                  [2] The type parameter is the event name *without* the 'on' prefix.

                  --
                  Michael Winter
                  Replace ".invalid" with ".uk" to reply by e-mail.

                  Comment

                  Working...