Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!