473,378 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Object Access By Reference?

OK...I have a function that formats some form input data. When I run the function I save the form object in an array...So far so good.

Now when I am done with my page I want to go back and remove the formatting from the objects. Should be easy since I saved those objects previously, but it appears that what I have is a copy of those objects and not a reference to those objects. So anything I try to update at the end does nothing to the actual object. At least I do not see a change in the DOM. Is there a way to do this without having to crawl all over the DOM to update the values?

Expand|Select|Wrap|Line Numbers
  1. {
  2. var arDone = new Array;
  3.  
  4. function formatCurrency(obj) {
  5.   var num = (parseInt(stripNumber(obj.value) * 100 + .5) / 100).toFixed(2);
  6.  
  7.   while(num.match(/\d{4}[\.\,]/)) {
  8.     num = num.replace(/(\d+)(\d{3})/, '$1,$2');
  9.   }
  10.  
  11.   obj.value = num;
  12.   arDone.push(obj);
  13.  
  14.   return num;
  15. }    
  16.  
  17. function cleanCommas() {
  18.   var obj;
  19.   for (var i = 0; i < arDone.length; i++) {
  20.     arDone.value = arDone[i].value.replace(/\,/g, '');
  21.  
  22. alert(arDone.value);
  23.   }
  24.   alert(document.form1.mortgage1_balance.value);
  25.   return 1;
  26. }
  27.  
  28. }
Feb 7 '08 #1
4 1128
acoder
16,027 Expert Mod 8TB
Surely arDone.value should be arDone[i].value?
Feb 7 '08 #2
Surely arDone.value should be arDone[i].value?
DOH!!!!!!!

Never try to code after 48 hours without sleep.....Thanks...You are of course correct, and everything works just beautiful when you do that...

Problem solved....

In the interest of giving back to the community, here is the finished code...

Expand|Select|Wrap|Line Numbers
  1. {
  2.   var arDone = new Array;
  3.  
  4.   function stripNumber(num) {
  5.     return num.toString().replace(/[^\d\.-]/g, '');
  6.   }
  7.  
  8.   function formatCurrency(obj) {
  9.     var num = (parseInt(stripNumber(obj.value) * 100 + .5) / 100).toFixed(2);
  10.  
  11.     while(num.match(/\d{4}[\.\,]/)) {
  12.       num = num.replace(/(\d+)(\d{3})/, '$1,$2');
  13.     }
  14.  
  15.     obj.value = num;
  16.     arDone.push(obj);
  17.  
  18.     return num;
  19.   }    
  20.  
  21.   function cleanCommas() {
  22.     for (var i = 0; i < arDone.length; i++) {
  23.       arDone[i].value = arDone[i].value.replace(/\,/g, '');
  24.     }
  25.  
  26.     return 1;
  27.   }
  28.  
  29. }
Feb 7 '08 #3
acoder
16,027 Expert Mod 8TB
Thanks for posting the solution. Post again if you have more questions.
Feb 8 '08 #4
No prob...Happy to help, for those who missed it, The solution includes a killer money formatting algorithm...behold the power of regular expressions...

StripNumber removes all characters except for numbers, periods (for decimal point), and hyphens (for negative sign). This could be made more specific by removing any hyphens not on the front of the number or only allowing the first period, but this was good enough for what I was doing.

FormatCurrency begins by stripping the number, then to accomodate 2 decimal place accuracy it multiplies by 100 and in order to round up it adds .5 and then drops anything after the decimal point. Next, it returns the 2 digits after the decimal by dividing by 100 and to make it display pretty, uses toFixed to add any trailing zeros to the output.

The last step is to add commas in the thousands, millions, billions, etc. position. It does this by looking for 4 decimal digits in front of a comma or period. If it finds the four digits, it replaces them with whatever digits come first, followed by a comma and then the trailing 3 digits. It repeats this until you do not have four digits in a row.

You could also include a preceeding dollar sign at this point if desired, or you could wrap the entire number in parentheses if it is negative and just throw the hyphen away. Your mileage may vary.

Finally it assigns the formatted number back to the object.
That's it! No ginsu knives or shipping and handling...Just clean simple code thank to the genius who invented regular expressions.

If you want to thank me for this little gem, take a minute and help the next guy who comes along. What goes around comes around. The world is a better place if we all lend a hand.

The following is the javascript code followed by a sample use in an html form:

Expand|Select|Wrap|Line Numbers
  1.   function stripNumber(num) {
  2.     return num.toString().replace(/[^\d\.-]/g, ''); 
  3.   }
  4.  
  5.   function formatCurrency(obj) {
  6.     var num = (parseInt(stripNumber(obj.value) * 100 + .5) / 100).toFixed(2);
  7.  
  8.     while(num.match(/\d{4}[\.\,]/)) {
  9.       num = num.replace(/(\d+)(\d{3})/, '$1,$2');
  10.     }
  11.  
  12.     obj.value = num;
  13.   } 
----- HTML ------
<input type="text" name="money" onChange="formatCurrency(this);" />
Feb 11 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Chris S. | last post by:
I'm trying to make a graphical editor and browser for Pickled files. One aspect I'm not sure about is how to detect multiple references to the same data. For instance, say I had the Pickled...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
29
by: Tim Clacy | last post by:
Am I correct in think that you can't re-assign a reference to a different object? If so, what should happen here: struct A { DoSomeThing(); }; A& GetNextA();
1
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
3
by: Grant Schenck | last post by:
Hello, I have a Windows Service developed in C# .NET. I'm making it a remote server and I can, via an IPC Channel, expose methods and call them from a client. However, I now want my remoted...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.