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

129
Vistas
Building a keyboard and missing onclick event

I'm building a virtual keyboard with vanillla javascript but don't know where to add the onclick event listener to the buttons or how to grab them. I have a printKeys function that loops thru the array and prints them onload, and I have an unfinished typeKeys function where I'm trying to grab the innerhtml and print it to the input field.

HTML

</head>          
<body onload="printKeys()">
        <div class="text">
          <input type="text" class="your-text" id="input" placeholder="Your text here.."></input>
    
          <button class="copy-btn">Copy</button>
        </div>
        <div class="keyboard" id="keyboard"></div>
    
        <script src="index.js"></script>
      </body>
    </html>

Javascript

    const alphaKeys = ["a","b","c"];
    const numKeys = "1234567890";
    const keyboard = document.getElementById("keyboard");
    
    // render keyboard
    function printKeys() {
      for (let i = 0; i < alphaKeys.length; i++) {
        let keys = document.createElement("button");
        keys.innerHTML = alphaKeys[i];
        //add onclick function to button
        keyboard.appendChild(keys);
      }
    }
    
    //onClick event, add text in text field
    const input = document.getElementById('input')
    
    function typeKeys() {
      console.log("clicked")
      //grab input and replace with button innerhtml
    }

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Instead of adding the event handler to each button, you can apply it to the parent (keyboard) then just use the event's target to get the specific button. I also added the character to a data-attribute instead of the innerHTML.

const alphaKeys = ["a","b","c"];
    const numKeys = "1234567890";
    const keyboard = document.querySelector(".keyboard");
    
    // render keyboard
    function printKeys() {
      for (let i = 0; i < alphaKeys.length; i++) {
        let keys = document.createElement("button");
        keys.innerHTML = alphaKeys[i];
        keys.setAttribute("data-character",alphaKeys[i]);
        keyboard.appendChild(keys);
      }
    }
    
    //onClick event, add text in text field
    const input = document.getElementById('input')
    
    function typeKeys(character) {
        input.value += character;
    }

keyboard.addEventListener("click",function(e){
  let target = e.target;
  if(target.getAttribute("data-character")){
     typeKeys(target.getAttribute("data-character"))
  }
});

printKeys();
<div class="text">
          <input type="text" class="your-text" id="input" placeholder="Your text here..">
          <button class="copy-btn">Copy</button>
        </div>
        <div class="keyboard" id="keyboard"></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