0

after several tries (especially this question) I still can't get the text that the user enters in my inputs :

<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link type="text/css" rel="stylesheet" href="css1.css">
        <link type="text/css" rel="stylesheet" href="login.css">
        <script src="js/jquery-2.1.1.min.js"></script>
    </head>
    <body>
        <div id="firstForm">
            <h2>Login</h2>
            <form id="signin" action="index.html" method='post'>
                <input id='login1' class="inputs" name="mail" type="email" placeholder="mail" required>
                <input id='password1' class="inputs" name="password" type="password" placeholder="password" required><br>
                <input class="inputs" type="submit" id="submitLogin">
            </form>
        </div>
</body>
    <script src='js/loginscript.js'></script>
</html>   

JS :

(function (){
        var login=document.getElementById("login1").value;
        var pwd=document.getElementById("password1").value;
        console.log(login + '   *   '+ pwd);
        document.getElementById('signin').addEventListener('submit',function(e){
            console.log(login + '   *   '+ pwd);
            e.preventDefault();
        },false);
    })();

Both console logs print out the * with blanks : nothing was caught...

Thank you!

Community
  • 1
  • 1
ted
  • 13,596
  • 9
  • 65
  • 107
  • You're calling the console logs right after you get to the `script` tag. I think what you wanted was to attach that function to the submit button. – Zaenille Aug 06 '14 at 06:03
  • @MarkGabriel Gabriel, wrong, he already added document.getElementById('signin').addEventListener('submit',function(e){ – Se0ng11 Aug 06 '14 at 06:05

1 Answers1

1

You have evaluated the login and pwd even before the user has interacted with it.

Please refer the fiddle. Updated JavaScript:

(function (){
    var login = document.getElementById("login1"), 
        pwd = document.getElementById("password1");
    document.getElementById('signin').addEventListener('submit',function(e){
        e.preventDefault();
        console.log(login.value + '   *   '+ pwd.value);
    },false);
})();
Sarbbottam
  • 5,410
  • 4
  • 30
  • 41