Capturing the text within an input field
up vote
0
down vote
favorite
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
|
show 1 more comment
up vote
0
down vote
favorite
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
when do you call that javascript code? once the page is loaded? or everything there is a change in the input?
– Dekel
Nov 20 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 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 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 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 at 1:44
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
javascript html
edited Nov 20 at 1:47
asked Nov 20 at 1:37
Dalan Ienatsch
123
123
when do you call that javascript code? once the page is loaded? or everything there is a change in the input?
– Dekel
Nov 20 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 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 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 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 at 1:44
|
show 1 more comment
when do you call that javascript code? once the page is loaded? or everything there is a change in the input?
– Dekel
Nov 20 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 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 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 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 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 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 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 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 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 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 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 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 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 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 at 1:44
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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>
add a comment |
up vote
0
down vote
accepted
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>
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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>
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>
answered Nov 20 at 1:46
Randy Casburn
4,0751218
4,0751218
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
when do you call that javascript code? once the page is loaded? or everything there is a change in the input?
– Dekel
Nov 20 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 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 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 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 at 1:44