I have a quick and straightforward inquiry about JavaScript and the process of logging

I've been working on this JavaScript code that is supposed to log the information entered into the email and password fields onto the console, so I can later store it in a database.

The issue I'm facing is that when I run the code for the first time, everything works perfectly. However, on subsequent runs, only the last console.log() statement gets displayed.

I would really appreciate any help or advice on how to resolve this problem.

let email;
let password;

document.getElementById("loginBtn").onclick = function(){
  email = document.getElementById("email").value;
  console.log(email);
  password = document.getElementById("password").value;
console.log(password);
}
<div class="container" > 
    <h3 id="LogInToAcc" class="fonts"> Log into your account.</h3> 
    <hr>
   <p class="fonts" id="accDont">Don't have an account?</p>
   <a class="fonts" id="SignUp" href="signup.html"> Sign up!</a><br><br>
    
    
 <label class="inputlabel" for="email">Email:</label> <br> 

 <input  id="email" class="inputs" type="email" > <br><br>

 <label class="inputlabel" for="password"  >Password:</label> <br>

 <input id="password" class="inputs"  type="password" pattern=".{5,10}"> <br><br>
 <a  class="forgot"  href="newpassword">Forgot your password?</a>
 <a href="">
 <button id="loginBtn" class="loginBtn" > Login</button> <br><br>
</a>

</div>

<script src="login.js"></script>   

I've tried different approaches like rearranging the code and declaring variables inside the function, but the issue persists.

Answer №1

It appears that your HTML code is not valid. By restructuring it and making some adjustments to the JavaScript, you can achieve a result similar to the example provided below.

Make sure to carefully consider the comments included in the code.

// To optimize performance, obtain element references once instead of every function call. Also, avoid setting variables to specific element properties to allow for flexibility in future property access.
const email = document.getElementById("email");
const password = document.getElementById("password");

// Implement event handling using modern standards
document.getElementById("loginBtn").addEventListener("click", function(){
  console.log(email.value, password.value);
});
<div class="container">
  <h1 id="LogInToAcc" class="fonts"> Log into your account.</h1>
  <p class="fonts" id="accDont">Don't have an account?</p>
  <p><a class="fonts" id="SignUp" href="signup.html"> Sign up!</a></p>
  <div><label class="inputlabel" for="email">Email:</label></div>
  <div><input  id="email" class="inputs" type="email"></div>
  <div><label class="inputlabel" for="password"  >Password:</label></div>
  <div><input id="password" class="inputs"  type="password" pattern=".{5,10}"></div>
  <div><a class="forgot"  href="newpassword">Forgot your password?</a></div>
  <div><button id="loginBtn" class="loginBtn" > Login</button></div>
</div>

<script src="login.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Nested foreach loops interacting with one another

I am attempting to execute multiple nested foreach loops and stop at 12 iterations, however the current code I have is not functioning as expected. While it displays the desired output, there seems to be an issue where only the first image in each director ...

The Urdu font is experiencing issues with its right-to-left direction functionality

When displaying Urdu text in an HTML tag (span), the text is supposed to be written from right to left. To achieve this, I added direction:rtl to the CSS styling. However, there seems to be an issue as on the last line, it leaves empty space on the right. ...

Consistently shifting the Aframe camera towards the focal point

Is there a way to continuously adjust the view of my Aframe scene based on the current viewing direction? It seems like there are two key components needed for this: (1) obtaining the current viewing direction in the correct format, and (2) creating and u ...

Is it possible to retrieve the value of a span in a PHP contact form instead of an input?

I am in the process of setting up a contact form, and I have some valuable information saved within certain spans. Specifically, it's related to an ecommerce shopping basket. As the built-in checkout system is proving to be subpar, we are opting for a ...

Setting up a new event handler for an ng-repeat element

I am completely new to Angular! I want to implement a simple event in a form element created by ng-repeat with a specific value. Here is the HTML code: <div class="labels"> <div class="checkbox-element" ng-repeat="suggestN ...

Why is my CSS not showing up in Live Server when I preview it from Visual Studio?

Every time I use the "Go Live" option with Live Server (Visual Studio extension), I only get a preview of my plain HTML file. However, when I manually open the HTML file from the folder containing both HTML and CSS files, the page loads correctly with the ...

JavaFX, a versatile container capable of smoothly transitioning between two panes

Looking for a JavaFX container in my Desktop Application that can display a listview with flexible dimensions and transition to gridpane on row selection. The gridpane will show details of the selected row, and clicking the back button will revert to the ...

Use React to dynamically render a table by mapping over an array to generate

I'm facing an issue where I am trying to display an array within a specific section of a table, but it's not displaying. Even though all the values are being fetched correctly. const renderReturnReasons = () => { dailyReportDetailItem.re ...

Adding a fresh HTML element with jQuery

My friend and I are currently working on a userscript for a website but have hit a roadblock. Our goal is to add a script from the site's shoutbox to display names on the right-hand side so that users can "@" mention others with their names. The scri ...

I seem to be having trouble locating the correct file location

Apologies for the simplicity of my question. I've been struggling to include the find.js file in my articles.js file. Despite trying multiple variations, I can't seem to get the pathname right and haven't found a solution online. Can someon ...

When trying to link "/%PUBLIC_URL%/manifest.json" within the workspace, the process failed due to an incorrectly formatted URI while operating on Netlify

My website is encountering an error when it goes live, but the error does not appear in local host, only for the live build. I've been trying to find a solution without success. Any help would be greatly appreciated! [error shown on inspecting the web ...

Display 'Div 1' and hide 'Div 2' when clicked, then reveal 'Div 2' and hide 'Div 1' when a different button is clicked

I need help figuring out how to make two separate buttons work. One button should display 'Div 1' and hide 'Div 2', while the other button should show 'Div 2' and hide 'Div 1' when clicked. My knowledge of jquery an ...

Triple-decker Angular Table CSS Layout

Please take a look at the code below, it is currently working properly. <div ng-repeat="download in productDownloads" class="container"> <div class="row"> <div class="cell"> <strong>{{download.productName}}</strong> </di ...

Steps to execute a download function in jQuery for retrieving an audio file

I am facing a challenge in creating a download button that should, upon clicking, locate the URL of the current playing song and initiate the download process. However, pressing the button currently opens the file location instead of downloading it. I am a ...

Activate a specific component of hover effect within multiple components generated by the `v-for` loop

Hey there! I'm currently facing a CSS challenge related to Vue. Let's say I want to display multiple components using v-for, and I want to add a hover effect to each component individually. The goal is to have the hover effect only apply to the c ...

Phonegap app unable to process AJAX post request, functions properly in browser

My AJAX request is encountering issues specifically when tested on the Phonegap App using iOS10. The code I am working with is as follows and functions perfectly in a browser: if($.trim(firstname).length > 0 & $.trim(email).length > 0 & $.t ...

"Creating a function within a knockout viewmodel that is populated with JSON data: A step-by-step guide

Struggling with defining a function inside my viewmodel. I retrieve json data using jquery getJSON and then map it to the viewmodel. $.getJSON('/Company/GetCompanies', function(data) { var viewModel = new CompanyViewModel() viewModel.m ...

Enter the [color] HTML5 code and check for compatibility with all the latest web browsers

I am interested in implementing a color picker functionality using the input[color] element in HTML5. I would like to confirm if this method is compatible with all modern browsers. Additionally, I am curious if there are alternative ways to achieve this, ...

Invoke a Node.js script from a Spring Boot application to pass a Java object to the script. The script will modify the object and then return it back to the originating class

class Services { Address address = new Address(....); /* Invoke NodeJs script and pass address object In the js script modify address object var address = getAddress() Modify address object Return address obj ...

Javascript is failing to fetch the desired text

I'm in the process of tweaking the code found at 'http://jsfiddle.net/vivin/RjqUf/' to allow users to select a line or word from a PDF. I've added the following JavaScript snippet at the end of the existing code: $(".textLayer").mouseu ...