Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda