luissimo luissimo - 3 months ago 44
HTML Question

Replace comma with a dot while typing jQuery

Hi i want to replace a comma with a dot while typing in a text input field using jQuery. I have this code right now;

$(document).on('change', '.unitprice', function() {
$(this).val().replace(/,/g, '.');
});



The class of the input field is unitprice


But it's not working.. And i can't seem to find the correct answer on google. Anybody has an idea on how to accomplish this?

Answer

You're just changing the variable and not writing it back.

$(this).val($(this).val().replace(/,/g, '.'));