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

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

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 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