Struggling to grasp the concept of DOM Event Listeners

Hello, I have a question regarding some JavaScript code that I am struggling with.

function login() {
    var lgin = document.getElementById("logIn");
    lgin.style.display = "block";
    lgin.style.position = "fixed";
    lgin.style.width = "100%";
    lgin.style.height = "100%";
    lgin.style.paddingTop = "15px";
}

var lgButton = document.getElementById("openRooms");
lgButton.addEventListener("click", login, false);
<p><a href="#" id="openRooms">Log In</a></p>

<section id="logIn">
  <div class="form-style-6">
    <h3>Log In</h3>
    <form>
      <input type="text" name="field1" placeholder="Name" />
      <input type="email" name="field2" placeholder="Email Address" />
      <input type="password" name="field3" placeholder="Password" />
      <input type="submit" value="Submit" />
    </form>
     </div>
</section>

I am facing an issue where the JavaScript code I wrote is not functioning as expected.

If anyone could help me troubleshoot this, I would greatly appreciate it.

Answer №1

CSS Styling:

#loginForm.active {
  display: block;
  position: fixed;
  width: 100%;
  height: 100%;
  padding-top: 15%;
}

JavaScript Function:

document.addEventListener("DOMContentLoaded", function(event) { 
   function showLoginForm() {
     document.getElementById("loginForm").setAttribute("class", "active");
   }

   var loginButton = document.getElementById("openLoginPage");
   loginButton.addEventListener("click", showLoginForm, false);
});

Answer №2

Ensure to attach your event handlers during the window load event.

window.addEventListener("load",function(){
  var btnLogin = document.getElementById("signInButton");
  btnLogin.addEventListener("click", signInUser, false);    
},false);

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

Getting Data from Selected Radio Buttons in AngularJS Using ng-repeat

I am looking to retrieve the data (id, name) of a selected row using a radio button. Here is what I have so far: <tr ng-repeat="list in listTypes"> <td>{{list.TaskId}}</td> <td>{{list.Comments}}</td> <td>{{l ...

Is it possible to manipulate content using jQuery?

I need help figuring out how to update the content within a span tag when a user clicks on a button. The specific span tag I am trying to target has a class called fa_button_id_counter and an id that corresponds to the post id. Despite my efforts, I haven& ...

Console command to change paragraph tag color---Modifying the color

I am attempting to change the color of all paragraph tags on a webpage to white by utilizing the console. My initial attempt was: document.p.style.color = 'white'; However, this method did not have the desired effect. Interestingly, I have had s ...

Locate a specific data point within an array of JSON objects

After receiving an array of JSON objects from JSP, I now have a set of data that contains book titles. "Titles":[ { "Book3" : "BULLETIN 3" } , { "Book1" : "BULLETIN 1" } , { "Book2" : "B ...

Incorporate the slick-slider functionality smoothly into a WordPress website by utilizing JavaScript

I am fairly new to .js and currently attempting to incorporate a .js slider into my Wordpress site. I have been working on getting Slick-Slider operational on a website. While it functions perfectly fine on my static webpage, the transition to Wordpress ha ...

How can you tell if a specific keyboard key is being pressed along with the CTRL button?

Is there a way to call functions when a specific key is pressed along with the CTRL key (on a Windows system)? While testing for a particular keyCode, I used event.keyCode. I researched the codes assigned to each key and assumed that 17 + 73 would represe ...

Each block in Svelte includes a unique shorthand attribute

I have a JSON variable that holds attributes in a specific structure: // This json variable defines the attributes for elements to be created let myElements = [ { attr1: "myAttr1", attr2: "myAttr2", }, { ...

I'm experiencing trouble with Shopify as it appears to be failing to execute

I have been attempting to incorporate a tracking pixel into my project. Thus far, I have tested the code in various environments, both with and without wrapping it in a function and deploying it using window.onload. If you would like to see the implementa ...

Attempting to transpile JavaScript or TypeScript files for compatibility within a Node environment

Our node environment requires that our JavaScript files undergo Babel processing. Figuring out how to handle this has been manageable. The challenge lies in the fact that we have a mix of file types including .js, .jsx, .ts, and .tsx, which is not subject ...

The API response indicates that the service is currently not accessible

I'm attempting to retrieve a report from the MOZ API, but I keep receiving this response: { "status" : "503", "error_message" : "Service Temporarily Unavailable" } This is the code I am using: function MozCall(callback) { var mozCall = ' ...

Having trouble bringing in icons from the @mui/icons-material library

An error occurs when attempting to run the code. wait - compiling... error - ../../../../../node_modules/@mui/icons-material/esm/utils/createSvgIcon.js:1:0 Module not found: Can't resolve '@mui/material/utils' null Note: @mui/material pack ...

Is there a way to update the background dynamically?

I'm currently developing a weather application and I want the background to change dynamically based on the data retrieved from an API. Initially, I set up a variable and utilized an if statement for this purpose. However, I encountered difficulty in ...

Angular 2 communication between parent and child components

I am having trouble incorporating an action button in the child_1 component, while the event handler is located in the sub child component, child_2. Here's a snippet of the code: app.component.html (Parent HTML) <div style="text-align:center"> ...

Creating images or PDFs from HTML with CSS filters: a guide

Looking for someone who has experience creating images or PDFs from HTML code. The HTML contains images with CSS filters such as sepia and grayscale. If you have worked on this type of project before, I would love to hear about your experience. <img cl ...

No visible changes resulting from the CSS file

My project includes a CSS file named default.css, but it doesn't seem to be working or making any changes to my page This is the content of default.css: .feedbackText{ display: none; text-align: center; margin-left: 50px; color: blue; } In the HTML ...

Submitting information via jQuery

https://i.stack.imgur.com/VU5LT.jpg Currently, I am working on integrating an event planner into my HTML form. However, I am facing difficulty in transferring the data ("meetup", "startEvent", "break") from HTML to my database. Using jQuery, I was able to ...

Trigger a modal from one sibling Angular component to another

My application utilizes an Angular6 component architecture with the following components: <app-navbar></app-navbar> <app-dashboard></app-dashboard> The Dashboard component consists of: <app-meseros> </app-meseros> < ...

Struggling to convert a JSON file into a TableView within a JavaScript application developed with Appcelerator

Trying to display a JSON file in a table using JavaScript and Appcelerator is proving to be quite a challenge. The output appears as an empty table when compiled to an example page. As someone relatively new to JavaScript and JSON, I'm seeking guidanc ...

Navigate to the end of the progress bar once finished

I have a solution that works, but it's not very aesthetically pleasing. Here is the idea: Display a progress bar before making an ajax call Move the progress bar to the end once the call is complete (or fails) Keep the progress bar at 90% if the aj ...

Can fetch be used to retrieve multiple sets of data at once?

Can fetch retrieve multiple data at once? In this scenario, I am fetching the value of 'inputDest' (email) and 'a' (name). My objective is to obtain both values and send them via email. const inputDest = document.querySelector('i ...