Search code examples
javascripthtmlinputtext

get written text in HTML input field in JS


I have the following code in html for a simple form

<main>
  <form role="form">
    <fieldset id="personal-information" name="personal-information">
      <legend>Personal Information</legend>

      <section class="usernameSection" name="first-name">
        <label for="username">username:*</label>
        <input
          id="username"
          type="text"
          name="textfield"
          placeholder="username"
        />
      </section>
      <br />
      <section class="passwordSection" name="passwordSection">
        <label for="password">password:*</label>
        <input
          id="password"
          type="password"
          name="password"
          placeholder="password"
        />
      </section>
      <section class="submit" name="sumbit">
        <button id="submitButton" type='button' name="submit button"/>Sign in</button>
      </section>
    </fieldset>
  </form>
</main>

and in js I have the following code:

function signIn(userName, password) {
  console.log(userName,password)
}

const currentUsername = document.getElementById("username");
const currentPassword = document.getElementById("password");
const submitButton = document.getElementById("submitButton");

submitButton.addEventListener("click", () => {
  signIn(currentUsername.innerText, currentPassword.innerText);
});

The idea is just to log the username and passsword the users put in this field, but whenever I do this the console prints "" ""

Am I getting the input incorrectly by using .innerText?

Thanks in advance


Solution

  • You should use .value instead of .innerText.

    function signIn(userName, password) {
      console.log(userName,password)
    }
    
    const currentUsername = document.getElementById("username");
    const currentPassword = document.getElementById("password");
    const submitButton = document.getElementById("submitButton");
    
    submitButton.addEventListener("click", () => {
      signIn(currentUsername.value, currentPassword.value);
    });
    <main>
      <form role="form">
        <fieldset id="personal-information" name="personal-information">
          <legend>Personal Information</legend>
    
          <section class="usernameSection" name="first-name">
            <label for="username">username:*</label>
            <input
              id="username"
              type="text"
              name="textfield"
              placeholder="username"
            />
          </section>
          <br />
          <section class="passwordSection" name="passwordSection">
            <label for="password">password:*</label>
            <input
              id="password"
              type="password"
              name="password"
              placeholder="password"
            />
          </section>
          <section class="submit" name="sumbit">
            <button id="submitButton" type='button' name="submit button"/>Sign in</button>
          </section>
        </fieldset>
      </form>
    </main>