Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
JavaScript, programmatically alter a number input for when decimal dot is pressed

I am trying to make a custom keypad that can input a number with a fraction into a number input (important, not a text input).

However in chrome, when they use the keypad to type "0.", setting the value to "0." result in "0". However you can select the input and type "0." and it will accept it.

Before anyone points out the custom keycode and key attributes, its actually a web component in a shadow DOM, and one can add custom attributes in web components.

function clickHandler(event) {
    if (!event.target.hasAttribute('key')) return;
    const key = event.target.getAttribute('key')
    const keyCode = event.target.getAttribute('keycode')
    const input = document.getElementById("INPUT");
    input.focus();
    input.value = input.value + key;
} 
document.addEventListener('click', clickHandler);
#CONTAINER {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 32px;
}
button {
  padding: 4px;
}
<input type="number" id="INPUT" min="0" step="0.01">
<div id="CONTAINER">
    <button type="button" keycode="49" key="1">1</button>
    <button type="button" keycode="50" key="2">2</button>
    <button type="button" keycode="52" key="3">3</button>
    <button type="button" keycode="54" key="4">4</button>
    <button type="button" keycode="56" key="5">5</button>
    <button type="button" keycode="58" key="6">6</button>
    <button type="button" keycode="60" key="7">7</button>
    <button type="button" keycode="62" key="8">8</button>
    <button type="button" keycode="64" key="9">9</button>
    <button type="button" keycode="8"  key="Backspace">BS</button>
    <button type="button" keycode="48" key="0">0</button>
    <button type="button" keycode="110" key=".">.</button>
</div>

Try enter in the value "1.23".

I also tried what I think is superior, which is to simulate the keypress, but researching it, as usual, it is deemed insecure and not allowed:

const init = {
    key: key,
    code: key,
    charCode: Number(keyCode),
    keyCode: Number(keyCode),
    bubbles: true,
    cancelable: false,
    composed: true,
}
input.dispatchEvent(new KeyboardEvent('keypress', init));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use string input against number, and convert it to number if you need later to calculate with it.

function clickHandler(event) {
    if (!event.target.hasAttribute('key')) return;
    const key = event.target.getAttribute('key')
    const keyCode = event.target.getAttribute('keycode')
    const input = document.getElementById("INPUT");
    input.focus();
    input.value = input.value + key;
}

document.addEventListener('click', clickHandler);
#CONTAINER {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 32px;
}
button {
  padding: 4px;
}
<input type="STRING" id="INPUT" min="0" step="0.01">
<div id="CONTAINER">
    <button type="button" keycode="49" key="1">1</button>
    <button type="button" keycode="50" key="2">2</button>
    <button type="button" keycode="52" key="3">3</button>
    <button type="button" keycode="54" key="4">4</button>
    <button type="button" keycode="56" key="5">5</button>
    <button type="button" keycode="58" key="6">6</button>
    <button type="button" keycode="60" key="7">7</button>
    <button type="button" keycode="62" key="8">8</button>
    <button type="button" keycode="64" key="9">9</button>
    <button type="button" keycode="8"  key="Backspace">BS</button>
    <button type="button" keycode="48" key="0">0</button>
    <button type="button" keycode="110" key=".">.</button>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda