skip to Main Content

So I have this working codeblock in my script to replace the decimal seperator from comma “,” into period “.” ,when editing a form. Because in this region the decimal seperator comma is normal I also want the values to be displayed like this 1,99€ so I reverted the working function. The selected fields should change on load. When the form gets submitted I will cange it back again. For this example I show you only one of the fields.

The value=”1.5″ gets loaded from the Magento-Backend the wrong way, which is another story:

I included onload:"function(event)"
and window.onload = function(); to show two my attempts to adress this function from jQuery: jQuery('form').on('change', '#price', function(event) I also need to know how to remove the .on(‘change’ part. First time using Js und jQuery. I really tried everything.

 <html>
  <body onload="function(event)">
   <form>
    <input id="price" value="1.5">
   </form>
  </body>
 </html>

<script>

window.onload = function();

jQuery('form').on('change', '#price', function(event) {
   event.preventDefault();
   if (jQuery('#price').val().includes('.'))  {

    var varwithpoint = jQuery('#price').val();
    varwithcomma = varwithcomma.replace(",",".");

    jQuery('#price').val(varwithpoint);
} 
 else {
    console.log('no dot to replace');
}

});
</script>

2

Answers


  1. There were a few parts of the code which didn’t seem to be working as intended, so below is a basic example of code that will convert the “,” to a “.” if stored in the input “price”, and check this after each change of the value;

     
    function convert_price(){
      var this_price = $("#price").val();
      if (this_price.includes(',')) {      
        this_price = this_price.replace(",",".");
        $('#price').val(this_price);
      } else {
        console.dir('no dot to replace');
      }
    }
    convert_price();
    
    $("#price").on("change",convert_price);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <html>
      <body>
       <form>
        <input id="price" value="1,5">
       </form>
      </body>
     </html>
    Login or Signup to reply.
  2. I have called “init” the function that attaches the change event to the input file, I also changed the parameters passed to the on function

    function init(){
    
       var input = jQuery('#price');
       input.on('change', function(event) {
         event.preventDefault();
         var valueInInput = input.val();
         if (valueInInput.includes('.')) {
            var varwithcomma = valueInInput.replace(",",".");
            input.val(varwithcomma);
         } else {
            console.log('no dot to replace');
         }
       });
    }
    <html>
      <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      </head>
      <body onload="init()">
       <form>
        <input id="price" value="1.5">
       </form>
      </body>
     </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search