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: Wed, 11 Apr 2012 14:21:16 -0400 Organization: Aioe.org NNTP Server Lines: 56 Message-ID: <4F85CB9C.BB893EA6@erols.com> References: <4F85B0AE.A1E9FFD8@erols.com> NNTP-Posting-Host: 8p4YCYZc25dk5/DIdgWSVw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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:13337 Andreas Bergmaier wrote: > > Stan Weiss schrieb: > > save(stanForm.VD.value, AInput, 'Valve Diameter')" > > > > 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? > > JavaScript does not have call-by-reference. Instead of passing a > reference to the variable where the result should be stored, you pass > the actual value of AInput to the function, where it gets the value of > the local variable "vary". By assigning the result to vary, vary will > get a new value. AInput is forgotten. > > Also, you seem to try to parse a number with eval. Urghh. Use > parseFloat() instead. > > So, your code should be: > > function numeric(field, fallback, name) { > var num = parseFloat(field); > if (isNaN(num)) { > alert(name + " is not numeric"); > return fallback; > } else > return num; > } > AInput = numeric(stanForm.VD.value, AInput, 'Valve Diameter'); > > regards, > Bergi Thanks for the reply Bergi. It now return the correct value but does not function the way it did before. If a numeric and alpha are entered it will take the numeric part and use it. I want it to show any error if the field is not all numeric. So I changed if (isNaN(num)) { to if( isNaN( field ) ) Thanks again for your help Best Regards Stan