Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder1.xlned.com!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <4354243.e4iShcapur@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Wed, 11 Apr 2012 23:56:38 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: How to Pass variable name to a function Newsgroups: comp.lang.javascript References: <4F85B0AE.A1E9FFD8@erols.com> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 140 NNTP-Posting-Date: 11 Apr 2012 23:56:39 CEST NNTP-Posting-Host: 24718a4f.newsspool2.arcor-online.net X-Trace: DXC=ei5dPA9j1ic:i=48;n?Z:`A9EHlD;3Ycb4Fo<]lROoRa8kFnDZm8W4\YJNlJ`:FmdeL>K`fQ>EOE\[m`kk]CRX]:d8jc X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:13341 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 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 ), 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