Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #13341

Re: How to Pass variable name to a function

Message-ID <4354243.e4iShcapur@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-04-11 23:56 +0200
Subject Re: How to Pass variable name to a function
Newsgroups comp.lang.javascript
References <4F85B0AE.A1E9FFD8@erols.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Stan Weiss wrote:

> I have this code in a form which calls the save function.
> 
> <TR><TD ALIGN="RIGHT">
> <b>Valve Diameter in Millimetres </b>
> </TD><TD>
> <INPUT TYPE="text" NAME="VD" SIZE=7  onChange="save(stanForm.VD.value,
> AInput, 'Valve Diameter')">
> </TD></TR>
> 
> function save( field, vary, text ) {
>      if( isNaN( field ) )
>      {
>         alert( text + " is not numeric" );
>      }
>      else
>      {
>          AInput = eval(field);  // this Works
>          vary = eval(field);    // this does not workWorks
>      }
> }
> 
> Can anyone see what I am doing wrong?

Yes; it is (at least) the following:

1. Your referencing is proprietary, at best error-prone, needlessly.

     stanForm.VD.value

   is not appropriate referencing (throw that book away).  Assuming that
   your control is descendant of a form element named `stanForm' (which you
   have not showed), an appropriate, standards-compliant, backwards
   compatible and less error-prone referencing would be

     document.forms['stanForm'].elements['VD'].value

   However, as that code appears to be part of the event-handler attribute
   value of the very element that is represented by

     document.forms['stanForm'].elements['VD']

   you can replace that with

     this

   so that the element reads

     <input type="text" name="VD" size="7"
            onchange="save(this.value, AInput, 'Valve Diameter')">

2. You are referring to an identifier before you declare or define it.
   `AInput' is neither declared nor defined before save() is called, so for
   all we know,

     save(this.value, AInput, 'Valve Diameter')

   throws a ReferenceError when evaluating `AInput'.  Check your error
   console.

   It is possible that you have

     var AInput;

   somewhere in your code, or even

     <form name="stanForm" …>
       …
       <input name="AInput" …>
       …
     </form>

   (see also <http://stackoverflow.com/questions/9158238/why-js-function-
    name-conflicts-with-element-id/9160009#9160009>), but then you should
   have posted that.

3. You are using eval().

   Given the aforementioned `forms' and `elements' collections, you do not
   have to use eval() here.  See also <http://jibbering.com/faq/#eval> (in 
   general, *always* read the FAQ of the newsgroup(s) *before* you post to
   them).

4. You are using eval() wrong.

   You are passing for the argument of eval() – `field' – the value of the
   control that calls the very same function.  One could surmise from this
   that you might be trying to implement a calculator.
  
   However, this assumption contradicts your programming where you
   explicitly do not enter the branch with the eval() calls when the string
   value cannot be interpreted as a number, such as a non-trivial equation
   (operand operator operand).

   It is also rather far-fetched to assume that your user would enter
   the names of controls in your form in that input control.

   Therefore, your calls to eval() appear to be completely pointless.  To
   convert a string value to a number value, use the unary `+' operator,
   or the parseInt(…, radix) or parseFloat(…) function calls.

5. "Does not work" is not a problem description.  Read the FAQ on how to
   post when you desire useful answers.

6. You can assign to a function argument, but that does not change the
   value passed for that argument.  There is no call-by-reference, all
   function arguments are values.  (But it does not appear as if you were
   trying to use the assumed assigned value anywhere, so this appears
   to be pointless anyway.)

7. The `change' event occurs on the control when the control loses focus,
   not before.  Do not have your users rely on that, provide a button for
   the computation.  You may perform form validation on change, but you
   should do it in a non-obtrusive way (like adding color).
   Perform any validation that hinders or blocks the user's workflow (like
   an alert message) only on submit; the `form' element has an `onsubmit'
   event-handler attribute for that.

8. Avoid the window.alert() method.  In particular, in recent Gecko-based
   browsers, such an alert message disables the entire viewport of a tab
   until the message is dismissed, and it is very likely to cover the parts
   where the error is.  (IMHO yet another incredibly bad design decision
   in Geckos that we now need to work around – thanks guys :->)

9. Is your markup Valid?  Declare HTML 4.01 Strict or (if necessary) HTML5,
   and replace presentational attributes (like `align') and elements with
   CSS.  <http://validator.w3.org/>

Bottom line: Learn the markup and programming languages before you use an 
API with the programming language, and try something much more simple than 
this first.


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-11 12:26 -0400
  Re: How to Pass variable name to a function Andreas Bergmaier <andber93@web.de> - 2012-04-11 19:47 +0200
    Re: How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-11 14:21 -0400
      Re: How to Pass variable name to a function Stefan Weiss <krewecherl@gmail.com> - 2012-04-11 20:53 +0200
        Re: How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-11 15:16 -0400
          Re: How to Pass variable name to a function Gene Wirchenko <genew@ocis.net> - 2012-04-11 15:18 -0700
  Re: How to Pass variable name to a function "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-04-11 21:20 +0000
  Re: How to Pass variable name to a function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-11 23:56 +0200
    Re: How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-12 00:48 -0400
      Re: How to Pass variable name to a function Andrew Poulos <ap_prog@hotmail.com> - 2012-04-12 15:21 +1000
        Re: How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-12 02:11 -0400
          Re: How to Pass variable name to a function Andrew Poulos <ap_prog@hotmail.com> - 2012-04-12 18:57 +1000
            Re: How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-12 09:35 -0400
      Re: How to Pass variable name to a function Stefan Weiss <krewecherl@gmail.com> - 2012-04-12 08:08 +0200
        Re: How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-12 10:13 -0400
      Re: How to Pass variable name to a function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-12 22:47 +0200
        Re: How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-12 17:04 -0400
          Re: How to Pass variable name to a function Tim Streater <timstreater@greenbee.net> - 2012-04-12 22:17 +0100
          Re: How to Pass variable name to a function Scott Sauyet <scott.sauyet@gmail.com> - 2012-04-13 05:13 -0700
            Re: How to Pass variable name to a function "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-04-13 16:27 +0000
              Re: How to Pass variable name to a function Scott Sauyet <scott.sauyet@gmail.com> - 2012-04-16 04:11 -0700
        Re: How to Pass variable name to a function "Tom de Neef" <tdeneef@qolor.nl> - 2012-04-12 23:34 +0200
          Re: How to Pass variable name to a function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-04-13 10:03 +0200
          Re: How to Pass variable name to a function Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-04-13 12:03 +0200
            Re: How to Pass variable name to a function Andrew Poulos <ap_prog@hotmail.com> - 2012-04-13 21:35 +1000
            Re: How to Pass variable name to a function Gene Wirchenko <genew@ocis.net> - 2012-04-13 09:00 -0700
  Re: How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-12 14:03 -0400
  Re: How to Pass variable name to a function Dr J R Stockton <reply1215@merlyn.demon.co.uk.not.invalid> - 2012-04-12 20:06 +0100
    Re: How to Pass variable name to a function Stan Weiss <srweiss@erols.com> - 2012-04-12 18:54 -0400

csiph-web