javascript - Replacing comma with dot Char -


i have made calculation app in appjs.

basicly bunch of:

 <input type=number>  

fields.

to make more user friendly thought should replace commas dots, javascript can use actual values calculate.

i've tried doing this following pice of code:

$("input[type=number]").keyup(function(e){         var key = e.which ? e.which : event.keycode;         if(key == 110 || key == 188){           e.preventdefault();           var value = $(this).val();                    $(this).val(value.replace(",","."));         }    }); 

in explorer 9, works expected: see fiddle

but since app.js uses chromium guess thats happens in chromium. how can work around this?

this happens in app: when enter number containing comma char. comma char moved right , when input box loses focus, comma removed (probably since comma char isn't allowed in type=number)

when value of <input type=number> isn't valid, blank string returned. check doing this:

$("input[type=number]").keyup(function(e){         var key = e.which ? e.which : event.keycode;         if(key == 110 || key == 188){           e.preventdefault();           var value = $(this).val();            console.log(value === "");                   $(this).val(value.replace(",","."));         }    }); 

it print true every time. therefore, need to

  1. since, on keyup event, input has changed, must change keydown or keypress event.

  2. change value.replace(",", ".") value + "." (since there no ",").

  3. actually, need insert cursor is. i'll update when have time.

finished code:

$("input[type=number]").keydown(function (e) {     var key = e.which ? e.which : event.keycode;     if (key == 110 || key == 188) {         e.preventdefault();         var value = $(this).val();         console.log(value);         $(this).val(value + ".");     } }); 

a better idea might make <input type=text> , validate manually if need feature.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -