skip to Main Content

I have to code a website, I have my homepage and I want to add an input in my HTML and to add in my JavaScript a function who check if the word is the same in Javascript.

It just need to check if the input is "nsi", and then return "right".
This website is just for me, in local. So I don’t need php, sql or other language.

function secret() {
  var secretInput = document.getElementById("secret").value;
  var mot = "nsi";
  console.log("secretInput");
  if (secretInput === mot) {
    var bravo = document.getElementById('bravo');
    bravo.innerHTML = "right";
  }
}
<div class="secretword" id="english">
  <p>
    Clues have been placed on the different pages to form a word, find it!
    <input id="secret" autocomplete="off">
    <button onclick="secret()">Done</button>
  </p>
</div>

3

Answers


  1. You are missing an element with id="bravo"

    add for example

    <div id="bravo">
    </div>
    

    and it’ll work

    Login or Signup to reply.
  2. You haven’t called any tag in your HTML with the "bravo" tag, so JS won’t find any element with that id and will return an error:

    <div class="secretword" id="english">         
    <p>Clues have been placed on the different pages to form a word, find it! <input id="secret" autocomplete="off">
    <button onclick="secret()">Done</button>
    </p>
    <p id="bravo"></p>       
    </div>
    

    If you want to add a new paragraph tag for the action.

    Login or Signup to reply.
  3. Do you have an element in your html with the id "bravo" ? make sure to have that.

    <div id="bravo"></div>
    

    and in your console.log statement you are logging the string "secretInput" , if you want to log the variable’s value you should use secretInput without quotes

    console.log(secretInput);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search