skip to Main Content

I have this simple HTML/JS file that I’m working with. When I click the generate button, I should the output of the normalize function shown as text on the screen, but that’s not happening. Can someone help me figure out why the p element is not being updated?

function normalize(str) {
  output = str.toLowerCase();
  return output;
}

function generate() {
  input = normalize(document.getElementById("input").value);
  document.getElementById("output").textContents = input;
}
<div id="input_div">
  <input type="text" size="25" id="input">
  <input type="button" value="Generate" id="gen" onclick="generate()">
</div>
<p id="output"></p>

2

Answers


  1. A typo.

    The property you’re looking for is textContent not textContents.

    Login or Signup to reply.
  2. You can either use "textContent".

       document.getElementById("output").textContent = input;
    

    OR "innerHTML"

    document.getElementById("output").innerHTML = input;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search