HI All,
I have a piece of javaScript that removes commas from a provided string (in my case currency values)
It is:
function replaceCommaInCurrency(myField, val) { var re = /,/g; document.net1003Form.myField.value=val.replace(re, ''); }
‘MyField’ was my attempt to dynamically have this work on any field that I pass in, but it doesn’t work, I get errors saying ‘MyField’ is not valid. I sort of get my, but I thought this was valid.
I am calling by using: onBlur=’replaceCommaInCurrency(this.name, this.value);return false;’
this.name and this.value are passing in the right values…field name and its value.
How do I do this dynamically?
-Jason
You can use eval to make your code snippet work:
As mentioned below, the square brackets work (and don’t suck like eval), stupid me for forgetting about those:
Alternatively, try something like this:
Which gets called like so:
You should consider using a javascript toolkit for things like this. You could set a class like ‘currency’ on each input, then use this snippet of jQuery based Javascript to handle everything:
This code would fire on document ready, attach an event handler to each input with currency as its class, and then do the replacements. Note that you don’t need a regex for replacement as well.