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

213
Vistas
Capitalize every second letter of an input, then show the finished sentence on the site javascript html

I have a small project in which the user writes something into an input field. Then with javascript you capitalize every second letter of an input, then show the new string on the site. Ive been trying to assign tekstEndrer.innerHTML to the new string but its not working.



const tekst = document.getElementById("tekstInput").value;

document.querySelector('input[type=button]').addEventListener("click", function(){tekstEndrer();});

const tekstEndrer = () => {
  var res = "";
  for (let i = 0; i < tekst.length; i++) {
    res += i % 2 == 0 ? tekst.charAt(i).toUpperCase() : tekst.charAt(i);
  }
  document.getElementById("nyTekst").innerHTML = res;
}

<!doctype html>
<html>
<head>
    <title>Tekstformatering</title>
</head>

<body>
    <h1>tekst endrer</h1>
    <input type="text" id="tekstInput">
    <input type="button" value="Endre tekst!" id="knapp">
    <div id="nyTekst"> yoyo</div>
    
    <script type="text/javascript" src="tekstlek.js"></script>
</body>



</html>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You'll need to access the value inside the callback function for the addEventListener and then pass it in to the function as an argument as it's not going to be in scope if you do it outside of the callback:

document.addEventListener('DOMContentLoaded', () => {
  document.querySelector('input[type=button]').addEventListener("click", function(){
    const tekst = document.getElementById("tekstInput").value;
    tekstEndrer(tekst);
  });

  const tekstEndrer = (tekst) => {
    var res = "";
    for (let i = 0; i < tekst.length; i++) {
      res += i % 2 == 0 ? tekst.charAt(i).toUpperCase() : tekst.charAt(i);
    }
    document.getElementById("nyTekst").innerHTML = res;
  }
})
<input id="tekstInput">
<input type="button">
<div id="nyTekst"></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