-3

This is an algorithm to format the amounts wrote in jQuery:

jsfiddle

But, there are some bugs.

For example, if I write 900.800,00 and then I delete "9" the import remains 00.800,00. How can I fix it? I fixed many bug but I don't know how to fix this.

Another bug arrives from:

$(this).on("dragstart", function(e) {
     return false;
});

It is used to prevent the drag the text inside the input field. But, after an AJAX call this not works more. I should use something like $(document).on, but how exactly?

Can you find other bugs? Thank you!

Ionut
  • 10,249
  • 4
  • 33
  • 62
Dave
  • 1,208
  • 3
  • 26
  • 43
  • Your second question is answered fairly canonically here: http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements – David Nov 16 '15 at 15:50
  • If you need help with code, post a minimal example of code that doesn't work on the question. Links are pointless as they can go stale – Jaromanda X Nov 16 '15 at 21:32

2 Answers2

1

What you are looking is to format 900.800,00 but this number is really just 900800.00 (american-style).

Steps:

  1. Remove all number-group separator.
  2. Parse the number so that it is machine understandable.
  3. Format your number again

But beside that, this question has already been asked thousands of time before.

And... numeral.js shows us that it's a "Solved Problem" that you shouldn't try to solve it again.

Community
  • 1
  • 1
Maxime Rouiller
  • 13,212
  • 9
  • 53
  • 104
0

About the second bug Remember that any element create dynamically, won't be included in event handler.

I'll explain. If you have this event listener

$( ".clickable" ).click( function(e) {
  alert( "Clicked" );
});

If you add another element with the "clickable" class to the page, it won't recive the click event. To do that, you need to use the event delegation

$( document ).on( "click", ".clickable", function( e ) {
  alert( "Clicked" );
});

Now the click event will be locked to the document itself and propagated to all ".clickable" elements inside it. To have a "cleaner" environment you can bind the click event only on a certain container

$( "div.clickable_container" ).on( "click", ".clickable", funciton( e ) {
  alert( "Clicked" );
});
RiccardoC
  • 898
  • 5
  • 10