Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Stan Weiss Newsgroups: comp.lang.javascript Subject: Re: How to Pass variable name to a function Date: Thu, 12 Apr 2012 00:48:05 -0400 Organization: Aioe.org NNTP Server Lines: 157 Message-ID: <4F865E85.F8DBA81B@erols.com> References: <4F85B0AE.A1E9FFD8@erols.com> <4354243.e4iShcapur@PointedEars.de> NNTP-Posting-Host: 2ifjlkPmCExjqLSDCEOrLw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org X-Mailer: Mozilla 4.8 [en] (Windows NT 5.0; U) X-Accept-Language: en X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.javascript:13345 Thomas 'PointedEars' Lahn wrote: > > Stan Weiss wrote: > > > I have this code in a form which calls the save function. > > > > > > Valve Diameter in Millimetres > > > > > > > > > > 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 > > 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 > >
> … > > … >
> > (see also 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 (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. > > 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 Is this a moderated group? Are you the group moderator? I poster enough code to get the answer I needed. While you seem to want to make all kinds of ASSumptions about things that were not posted. Thanks for looking Stan PS Some old habits - presentational attributes (like `align') go back to when I used to do this back in 1997. You know what people tell me that those old scripts still work great.