Arhab Safwan
Arhab Safwan

Reputation: 23

How to make the output print input text?

My code run perfectly in console but not finding a way to print the output in the text file. Basically I am not finding a way to print the result in a input text file, run perfectly in console.

function reverse() {
    for (let i = text.length-1; i >= 0; --i){
        document.getElementById('output').innerHTML += text[i];
        console.log(text[i]);   
    }

}
<div class="container" style="background-color: rgb(170, 166, 178); margin-top: 50px;padding-bottom: 10px;">
    <h4>input</h4>
    <span id="out"></span>
    <input type="text" id="input">

    <button onclick="document.getElementById('input').value = ''">clear</button> <br> <br>

    <input type="button" onclick="reverse(document.getElementById('input').value)"value="submit">
    <p id="output"></p>
</div>

Upvotes: 2

Views: 90

Answers (2)

Youssouf Oumar
Youssouf Oumar

Reputation: 45883

A few mistakes you are making, your output span has id="out" and you are searching for output in your JavaScript. You are giving an argument to reverse that you are not using. You could simplify your code to this:

function reverse() {
  document.getElementById("output").textContent = document.getElementById(
    "input"
  ).value;
}
<div class="container" style="background-color: rgb(170, 166, 178); margin-top: 50px;padding-bottom: 10px;">
  <h4>input</h4>
  <span id="output"></span>
  <input type="text" id="input">
  <button onclick="document.getElementById('input').value = ''">clear</button> <br> <br>
  <input type="button" onclick="reverse()" value="submit">
  <p id="output"></p>
</div>

Upvotes: 0

Som Shekhar Mukherjee
Som Shekhar Mukherjee

Reputation: 8168

You need to accept the text that is being passed to the reverse function.

function reverse(text) {
  for (let i = text.length - 1; i >= 0; --i) {
    document.getElementById("output").innerHTML += text[i];
  }
}
<div class="container" style="background-color: rgb(170, 166, 178); margin-top: 50px;padding-bottom: 10px;">
  <h4>input</h4>
  <span id="out"></span>
  <input type="text" id="input">

  <button onclick="document.getElementById('input').value = ''">clear</button> <br> <br>

  <input type="button" onclick="reverse(document.getElementById('input').value)" value="submit">
  <p id="output"></p>
</div>

Upvotes: 1

Related Questions