0

I would like to have my code return the HTML input using an alert.

I tried to with this code, below, but the output was nothing. I am pretty sure that its not the button...

<center>
  <h3>Enter The Username And Password To Unlock This Computer</h3>
  <script>
    function incorrect() {
      var user = document.getElementById("username");
      var pass = document.getElementById("password");
      text = "the username '" + user + "' is incorrect, and the password '" + pass + "' is also incorrect"
      console.log(text)
      alert(text)
    }
  </script>
  <form>
    <h6>Username</h6><input type="text" id="username" name="username" value="">
    <h6>Password</h6><input type="text" id="password" name="password" value="">
    <br><button type="button" onclick="incorrect">Unlock</button>


  </form>
</center>

4
  • 1
    Try to change onclick="incorrect" to onclick="incorrect()" Commented Feb 13, 2020 at 21:20
  • An alert can't have inputs, it's just text. You can either make a custom alert that does what you want or you can use the standard alert to display messages. Commented Feb 13, 2020 at 21:21
  • <center> tag is deprecated for more than 10 years, it's use is not advisable and it can stop working at any time Commented Feb 13, 2020 at 21:23
  • Well, @CalvinNunes, it still works Commented Feb 13, 2020 at 22:10

3 Answers 3

1

Do you want the text that is being typed in or just the html input block?

<center>
  <h3>Enter The Username And Password To Unlock This Computer</h3>
  <script>
    function incorrect() {
      var user = document.getElementById("username").value;
      var pass = document.getElementById("password").value;
      text = "the username '" + user + "' is incorrect, and the password '" + pass + "' is also incorrect"
      console.log(text)
      alert(text)
    }
  </script>
  <form>
    <h6>Username</h6><input type="text" id="username" name="username" value="">
    <h6>Password</h6><input type="text" id="password" name="password" value="">
    <br><button type="button" onclick="incorrect()">Unlock</button>


  </form>
</center>

Sign up to request clarification or add additional context in comments.

1 Comment

The text that is being typed in please, @DCR, thank you!
0

the problem is about braces, you should use <br><button type="button" onclick="incorrect()">Unlock</button>

      function incorrect(){
         var user = document.getElementById("username");
        var pass = document.getElementById("password");
        text = "the username '"+user+"' is incorrect, and the password '"+pass+"' is also incorrect"
        console.log(text)
        alert(text)
      }
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <script src="script.js"></script>
    <center>
    <h3>Enter The Username And Password To Unlock This Computer</h3>

    <form>
      <h6>Username</h6><input type="text" id="username" name="username" value="">
      <h6>Password</h6><input type="text" id="password" name="password" value="">
      <br><button type="button" onclick="incorrect()">Unlock</button>


    </form>
    </center>
  </body>
</html>

4 Comments

They are also trying to concatenate [object HTMLInputElement] instead of using .value to extract the value.
I just tried it and it gave me “the username '[object HTMLInputElement]' is incorrect, and the password '[object HTMLInputElement]' is also incorrect”
@ThePythonCoder right, this is what you've coded in js function
@Zera question was why function is not called, not how to implement proper validation or fix error message
0
<input type="text" class="input">
<button class="button">sad</button>


let t = document.querySelector('.button');

t.addEventListener('click', function () {
                let y = document.querySelector('.input').value;
                console.log(y);
           });

3 Comments

Please ellaborate a little bit more your answer and tell OP how it works.
its easy. we take value from input let y = document.querySelector('.input').value; and do with this data what we want console.log(y);
@ВадимШляхов Please edit that explanation into your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.