I am pretty new to javascript, but I am trying the best I can and I could not find the answer or something that helped me.
I am trying to put spaces between thousands, but in different JS files. So I would like to define that function in an other file, so I can reuse it. This works:
var parts = item['effect-value'].toString().split("."); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, " "); parts.join("."); What I have tried is the following:
Global file:
function addSeparator(){ parts.toString().split("."); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, " "); parts.join("."); } or something like this…
$.fn.addSeparator = function(options) { var $this = this; var parts = options.replace; $this.html({ parts.toString().split("."); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, " "); parts.join("."); }) return $this; }; The other files:
var parts = item['effect-value']; addSeparator(parts); Thank you in advance.
.jsfunction in it on the pages that call it) you can simply make a call to the function like sovar parts = addSeparator(item['effect-value']);