title: javascript input mask currency
body: <p>i got this error on input.value and input.length:</p>
  <blockquote>
  <p>scripts.js:151 Uncaught TypeError: Cannot read property 'length' of
  undefined</p>
  </blockquote>
  <p>I'm trying to apply a currency mask to an input, but I'm getting errors</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function formatNumber(n) {
    // format number 1000000 to 1,234,567
    return n.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",")
  }
  
  function formatCurrency(input, blur) {
    // appends $ to value, validates decimal side
    // and puts cursor back in right position.
  
    // get input value
    var input_val = input.value;
    console.log(input)
    // don't validate empty input
    if (input_val === "") { return; }
  
    // original length
    var original_len = input_val.length;
  
    // initial caret position 
    var caret_pos = input.getAttribute("selectionStart");
  
    // check for decimal
    if (input_val.indexOf(".") &gt;= 0) {
  
      // get position of first decimal
      // this prevents multiple decimals from
      // being entered
      var decimal_pos = input_val.indexOf(".");
  
      // split number by decimal point
      var left_side = input_val.substring(0, decimal_pos);
      var right_side = input_val.substring(decimal_pos);
  
      // add commas to left side of number
      left_side = formatNumber(left_side);
  
      // validate right side
      right_side = formatNumber(right_side);
  
      // On blur make sure 2 numbers after decimal
      if (blur === "blur") {
        right_side += "00";
      }
  
      // Limit decimal to only 2 digits
      right_side = right_side.substring(0, 2);
  
      // join number by .
      input_val = "$" + left_side + "." + right_side;
  
    } else {
      // no decimal entered
      // add commas to number
      // remove all non-digits
      input_val = formatNumber(input_val);
      input_val = "$" + input_val;
  
      // final formatting
      if (blur === "blur") {
        input_val += ".00";
      }
    }
  
    // send updated string to input
    input.value(input_val);
  
    // put caret back in the right position
    var updated_len = input_val.length;
    caret_pos = updated_len - original_len + caret_pos;
    input[0].setSelectionRange(caret_pos, caret_pos);
  }
  (function initalize() {
    // IIFE method
    let input = document.querySelector("input[data-type='currency']")
    input.addEventListener("keyup", () =&gt; {
      formatCurrency(this);
    })
    input.addEventListener("keyup", () =&gt; {
      formatCurrency(this, "blur");
    })
  
  })();</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.textfield_label {
    font-family: "Poppins";
    color: #a3a3a3;
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 1.5px;
  }
  .material-textfield {
    font-family: "Poppins";
    position: relative;
    margin-bottom: 30px;
    color: #a3a3a3;
  }
  
  .material-textfield label {
    font-family: "Poppins";
    position: absolute;
    font-size: 1.2rem;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    background: #fff;
    border-radius: 5px;
    color: gray;
    padding: 0 0.3rem;
    margin: 0 0.5rem;
    transition: 0.1s ease-out;
    transform-origin: left top;
    pointer-events: none;
  }
  .material-textfield input {
    font-family: "Poppins";
    width: 100%;
    font-size: 1rem;
    outline: none;
    border: 1px solid #ffb24f;
    border-radius: 5px;
    background: #00416a;
    padding: 1rem 0.7rem;
    color: #ffb24f;
    transition: 0.1s ease-out;
  }
  .material-textfield::before {
    background: none;
    border: 1px solid #ffb24f;
    content: "";
    display: block;
    position: absolute;
    top: 2px;
    left: 2px;
    right: 2px;
    bottom: 2px;
    pointer-events: none;
  }
  .material-textfield input:focus {
    border-color: #ebb76e;
  }
  .material-textfield input:focus + label {
    color: #ebb76e;
    top: 0;
    transform: translateY(-50%) scale(0.9);
    background: linear-gradient(
      180deg,
      rgba(235, 235, 235, 1) 0%,
      rgba(235, 235, 235, 1) 50%,
      rgba(255, 255, 255, 1) 50%,
      rgba(255, 255, 255, 1) 100%
    );
  }
  .material-textfield input::placeholder{
    color:#ebb76e;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>          &lt;div class="input_renda"&gt;
              &lt;h3 class="textfield_label"&gt;
                value
              &lt;/h3&gt;
              &lt;div class="material-textfield"&gt;
                &lt;input type="text" name="currency-field" id="currency-field" pattern="^\$\d{1,3}(,\d{3})*(\.\d+)?$"
                  value="" data-type="currency" placeholder=" $1,000,000.00" /&gt;
              &lt;/div&gt;
            &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>but I don't know why the input. value is not working,
  why do I get the element with the query selector
  If anyone can help me with this I'm happy</p>