Capturing the text within an input field












0















I'm having trouble figuring out what I'm doing wrong in this code. I'm simply trying to set the user's input for username and password and store them in their respective variables.
For some reason the variables are always and empty string.
Could someone tell me where I'm going wrong?



HTML



<body>
<main>
<h1>Login Form</h1>
<form>
<label for="username">Username:</label>
<input type="text" name="username" id="username">
<label for="password">Password:</label>
<input type="text" name="password" id="password">
<button id="submitButton" type="button">Submit</button>
</form>
</main>
<script src="loginPage.js"></script>
</body>


JS



var submitButton = document.getElementById("submitButton");
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var user = {username, password};
console.log(user);









share|improve this question

























  • when do you call that javascript code? once the page is loaded? or everything there is a change in the input?

    – Dekel
    Nov 20 '18 at 1:39











  • set the user's input vs. store them in their respective variables || So do you want set the input's value attribute or are you trying to retrieve the input's value attribute value?

    – Randy Casburn
    Nov 20 '18 at 1:39











  • JS seems correct. Can you show us more of your code please? For example, when you actually try to read the values?

    – Matthew Herbst
    Nov 20 '18 at 1:40











  • I'm trying to retrieve the input's value. So say someone enters "joe" into the username field I want var username to equal "joe".

    – Dalan Ienatsch
    Nov 20 '18 at 1:42











  • There is nothing wrong with this code. Please show some more of your code, where you use it etc.

    – Abana Clara
    Nov 20 '18 at 1:44
















0















I'm having trouble figuring out what I'm doing wrong in this code. I'm simply trying to set the user's input for username and password and store them in their respective variables.
For some reason the variables are always and empty string.
Could someone tell me where I'm going wrong?



HTML



<body>
<main>
<h1>Login Form</h1>
<form>
<label for="username">Username:</label>
<input type="text" name="username" id="username">
<label for="password">Password:</label>
<input type="text" name="password" id="password">
<button id="submitButton" type="button">Submit</button>
</form>
</main>
<script src="loginPage.js"></script>
</body>


JS



var submitButton = document.getElementById("submitButton");
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var user = {username, password};
console.log(user);









share|improve this question

























  • when do you call that javascript code? once the page is loaded? or everything there is a change in the input?

    – Dekel
    Nov 20 '18 at 1:39











  • set the user's input vs. store them in their respective variables || So do you want set the input's value attribute or are you trying to retrieve the input's value attribute value?

    – Randy Casburn
    Nov 20 '18 at 1:39











  • JS seems correct. Can you show us more of your code please? For example, when you actually try to read the values?

    – Matthew Herbst
    Nov 20 '18 at 1:40











  • I'm trying to retrieve the input's value. So say someone enters "joe" into the username field I want var username to equal "joe".

    – Dalan Ienatsch
    Nov 20 '18 at 1:42











  • There is nothing wrong with this code. Please show some more of your code, where you use it etc.

    – Abana Clara
    Nov 20 '18 at 1:44














0












0








0








I'm having trouble figuring out what I'm doing wrong in this code. I'm simply trying to set the user's input for username and password and store them in their respective variables.
For some reason the variables are always and empty string.
Could someone tell me where I'm going wrong?



HTML



<body>
<main>
<h1>Login Form</h1>
<form>
<label for="username">Username:</label>
<input type="text" name="username" id="username">
<label for="password">Password:</label>
<input type="text" name="password" id="password">
<button id="submitButton" type="button">Submit</button>
</form>
</main>
<script src="loginPage.js"></script>
</body>


JS



var submitButton = document.getElementById("submitButton");
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var user = {username, password};
console.log(user);









share|improve this question
















I'm having trouble figuring out what I'm doing wrong in this code. I'm simply trying to set the user's input for username and password and store them in their respective variables.
For some reason the variables are always and empty string.
Could someone tell me where I'm going wrong?



HTML



<body>
<main>
<h1>Login Form</h1>
<form>
<label for="username">Username:</label>
<input type="text" name="username" id="username">
<label for="password">Password:</label>
<input type="text" name="password" id="password">
<button id="submitButton" type="button">Submit</button>
</form>
</main>
<script src="loginPage.js"></script>
</body>


JS



var submitButton = document.getElementById("submitButton");
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var user = {username, password};
console.log(user);






javascript html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 1:47







Dalan Ienatsch

















asked Nov 20 '18 at 1:37









Dalan IenatschDalan Ienatsch

143




143













  • when do you call that javascript code? once the page is loaded? or everything there is a change in the input?

    – Dekel
    Nov 20 '18 at 1:39











  • set the user's input vs. store them in their respective variables || So do you want set the input's value attribute or are you trying to retrieve the input's value attribute value?

    – Randy Casburn
    Nov 20 '18 at 1:39











  • JS seems correct. Can you show us more of your code please? For example, when you actually try to read the values?

    – Matthew Herbst
    Nov 20 '18 at 1:40











  • I'm trying to retrieve the input's value. So say someone enters "joe" into the username field I want var username to equal "joe".

    – Dalan Ienatsch
    Nov 20 '18 at 1:42











  • There is nothing wrong with this code. Please show some more of your code, where you use it etc.

    – Abana Clara
    Nov 20 '18 at 1:44



















  • when do you call that javascript code? once the page is loaded? or everything there is a change in the input?

    – Dekel
    Nov 20 '18 at 1:39











  • set the user's input vs. store them in their respective variables || So do you want set the input's value attribute or are you trying to retrieve the input's value attribute value?

    – Randy Casburn
    Nov 20 '18 at 1:39











  • JS seems correct. Can you show us more of your code please? For example, when you actually try to read the values?

    – Matthew Herbst
    Nov 20 '18 at 1:40











  • I'm trying to retrieve the input's value. So say someone enters "joe" into the username field I want var username to equal "joe".

    – Dalan Ienatsch
    Nov 20 '18 at 1:42











  • There is nothing wrong with this code. Please show some more of your code, where you use it etc.

    – Abana Clara
    Nov 20 '18 at 1:44

















when do you call that javascript code? once the page is loaded? or everything there is a change in the input?

– Dekel
Nov 20 '18 at 1:39





when do you call that javascript code? once the page is loaded? or everything there is a change in the input?

– Dekel
Nov 20 '18 at 1:39













set the user's input vs. store them in their respective variables || So do you want set the input's value attribute or are you trying to retrieve the input's value attribute value?

– Randy Casburn
Nov 20 '18 at 1:39





set the user's input vs. store them in their respective variables || So do you want set the input's value attribute or are you trying to retrieve the input's value attribute value?

– Randy Casburn
Nov 20 '18 at 1:39













JS seems correct. Can you show us more of your code please? For example, when you actually try to read the values?

– Matthew Herbst
Nov 20 '18 at 1:40





JS seems correct. Can you show us more of your code please? For example, when you actually try to read the values?

– Matthew Herbst
Nov 20 '18 at 1:40













I'm trying to retrieve the input's value. So say someone enters "joe" into the username field I want var username to equal "joe".

– Dalan Ienatsch
Nov 20 '18 at 1:42





I'm trying to retrieve the input's value. So say someone enters "joe" into the username field I want var username to equal "joe".

– Dalan Ienatsch
Nov 20 '18 at 1:42













There is nothing wrong with this code. Please show some more of your code, where you use it etc.

– Abana Clara
Nov 20 '18 at 1:44





There is nothing wrong with this code. Please show some more of your code, where you use it etc.

– Abana Clara
Nov 20 '18 at 1:44












1 Answer
1






active

oldest

votes


















0














Keep in mind, that the variables will only be populated after a change to the input.






document.querySelector('input[name:username').addEventListener('change',(e)=>{
var username = e.target.value;
}

document.querySelector('input[name:password').addEventListener('change',(e)=>{
var password = e.target.value;
}

<form>
<label for="username">Username:</label>
<input type="text" name="username" id="username">
<label for="password">Password:</label>
<input type="text" name="password" id="password">
</form>








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385029%2fcapturing-the-text-within-an-input-field%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Keep in mind, that the variables will only be populated after a change to the input.






    document.querySelector('input[name:username').addEventListener('change',(e)=>{
    var username = e.target.value;
    }

    document.querySelector('input[name:password').addEventListener('change',(e)=>{
    var password = e.target.value;
    }

    <form>
    <label for="username">Username:</label>
    <input type="text" name="username" id="username">
    <label for="password">Password:</label>
    <input type="text" name="password" id="password">
    </form>








    share|improve this answer




























      0














      Keep in mind, that the variables will only be populated after a change to the input.






      document.querySelector('input[name:username').addEventListener('change',(e)=>{
      var username = e.target.value;
      }

      document.querySelector('input[name:password').addEventListener('change',(e)=>{
      var password = e.target.value;
      }

      <form>
      <label for="username">Username:</label>
      <input type="text" name="username" id="username">
      <label for="password">Password:</label>
      <input type="text" name="password" id="password">
      </form>








      share|improve this answer


























        0












        0








        0







        Keep in mind, that the variables will only be populated after a change to the input.






        document.querySelector('input[name:username').addEventListener('change',(e)=>{
        var username = e.target.value;
        }

        document.querySelector('input[name:password').addEventListener('change',(e)=>{
        var password = e.target.value;
        }

        <form>
        <label for="username">Username:</label>
        <input type="text" name="username" id="username">
        <label for="password">Password:</label>
        <input type="text" name="password" id="password">
        </form>








        share|improve this answer













        Keep in mind, that the variables will only be populated after a change to the input.






        document.querySelector('input[name:username').addEventListener('change',(e)=>{
        var username = e.target.value;
        }

        document.querySelector('input[name:password').addEventListener('change',(e)=>{
        var password = e.target.value;
        }

        <form>
        <label for="username">Username:</label>
        <input type="text" name="username" id="username">
        <label for="password">Password:</label>
        <input type="text" name="password" id="password">
        </form>








        document.querySelector('input[name:username').addEventListener('change',(e)=>{
        var username = e.target.value;
        }

        document.querySelector('input[name:password').addEventListener('change',(e)=>{
        var password = e.target.value;
        }

        <form>
        <label for="username">Username:</label>
        <input type="text" name="username" id="username">
        <label for="password">Password:</label>
        <input type="text" name="password" id="password">
        </form>





        document.querySelector('input[name:username').addEventListener('change',(e)=>{
        var username = e.target.value;
        }

        document.querySelector('input[name:password').addEventListener('change',(e)=>{
        var password = e.target.value;
        }

        <form>
        <label for="username">Username:</label>
        <input type="text" name="username" id="username">
        <label for="password">Password:</label>
        <input type="text" name="password" id="password">
        </form>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 1:46









        Randy CasburnRandy Casburn

        4,3241318




        4,3241318






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385029%2fcapturing-the-text-within-an-input-field%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith