Toggle visibility of divs on button click

let loginBtn = document.getElementById("btn-login");
let loginDiv = document.getElementById("loglog");
let signupBtn = document.getElementById("btn-signup");
let signupDiv = document.getElementById("signMe");

loginBtn.addEventListener("click", () => {
    if (loginDiv.style.display === "none") {
        signupDiv.style.display = "block";
    } else {
        loginDiv.style.display = 'none';
    }
})
 <div id="loglog">
                <form id="wrap">
                <img class="userImage" src="user.png" alt="user">
                <input id="email" type="text" name="email" placeholder="E-mail">
                <input id="password" type="password" name="password" placeholder="Password">
                <button id="btn-login" type="button" onclick="login()">Login</button>
                <div id="warning-msg"></p>
                </form>
            </div>

            <div id="signMe">
                <form id="Create">
                <h2>Create Account</h2>
                <input id="firstName" type="text" placeholder="First Name">
                <input id="lastName" type="text" placeholder="Last Name">
                <input id="newEmail" type="text" placeholder="E-mail">
                <input id="newPassword" type="password" placeholder="Password">
                <button id="btn-signup" type="button" onclick="registerUser()">Sign Up</button>
                <div id="warning-msg">
                </form>
            </div>

Hello knowledgeable individuals, I am seeking assistance on displaying the signup button (div2) when clicking the login button (div1). Please note that the initial CSS display property for div2 is set to "none". Thank you for your help.

Answer №1

The JavaScript toggle function is a handy tool for switching classes on and off.

Check out this straightforward example:

let btn = document.getElementById("btn-login");
let div = document.getElementById("loglog");

btn.addEventListener("click", () => {
    div.classList.toggle('hide');
})
.hide {
  display: none;
}
<button id="btn-login">Unshow/show</button>
<div id="loglog">Stuff to unshow on button click and show on the next button click</div>

Answer №2

It is recommended to utilize the window.getComputedStyle() function to accurately access the style of an element.

When using the window.getComputedStyle() method, you will receive an object that contains all the CSS property values of the specified element. This includes styles from active stylesheets and resolved computed values. To access individual CSS properties, you can use APIs provided by the object or index with CSS property names.

let btn = document.getElementById("btn-login");
let div = document.getElementById("loglog");
let btn2 = document.getElementById("btn-signup");
let div2 = document.getElementById("signMe");

btn.addEventListener("click", () => {
console.log(div.style.display)
    if (window.getComputedStyle(div) === "none") {
        div2.style.display = "block";
    } else {
        div.style.display = 'none';
    }
});

For further information, refer to MDN

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

React's setState function seems to be unable to store File type values as properties

I'm currently developing a customized useForm hook to manage an object and support various input data types. I encountered an issue when attempting to save an uploaded image to the 'value' property - even though I can confirm that the value ...

Display a unique and unconventional image as a dynamic full-screen background, ensuring the entire image is visible using React/RedwoodJS

Looking for a somewhat unusual solution here - I want to use an image as a background with all edges of the picture visible at all times. Typically, I would use the "cover" setting for this, but that doesn't achieve what I need (image included). After ...

A guide on extracting specific text from a div class

<div class="col_5"> <br> <i class="phone"> :: Before </i> 0212 / 897645 <br> <i class="print"> ...

Struggling to extract information from HTML code through Python web scraping techniques

Hello there! I'm currently in the process of extracting dividend history data for a specific stock from a website using web scraping in Python. However, being new to Python, I'm facing some challenges in retrieving the data. Below is a snippet of ...

Steps to assign the identifier as the current index in the v-for iteration

Struggling to assign the id based on the index of a v-for loop. I attempted: <li v-for="(item, index) in items" v-bind:key="item.id"> <p>{{item.name}}</p> <input id= {{index}} type= "number"> < ...

What is the counterpart of Django's add filter in Jinja2?

I'm attempting to display an HTML structure like this using jinja2: <div> <tr> <td>{{ row.a|add:b}}</td> </tr> </div> However, I encountered an error expected token 'end of print statement&a ...

Error Message Missing from Google Cloud Function Response

In my Google Cloud function code, I have encountered an issue where the status changes to 400 when there is an error creating a new user. However, on the client side, I always receive an "Error: invalid-argument" message regardless of the actual error from ...

Struggling with hashtags and ampersands in Angular when making an HTTP request

Dealing with Special Characters # and & in Angular's http.get() Request URL Take a look at my code first. Angular Service let versionsearch = "&"; let strweeksearch = "%23"; this.http.get(this.apiUrl + 'GetVersionInfo?vehicleVersion=' + v ...

Is there a way in JavaScript to activate a web element by clicking on its center?

I have a webpage and I'm looking to simulate clicks using the console. I attempted to do so with the code snippet document.getElementById("myButtonId").click(), but it seems that the element only responds to clicks at its center location. Is there ano ...

What are some creative ways to animate Angular ng-switch for both entering and leaving transitions?

I have created an angular directive named 'cardViewer' that is designed to display images with animation. The directive enables the user to slide the image to the left when pressing the "prev" button and to the right when pressing the "next" butt ...

What is the best way to close an ajax page within the main page using a button?

I am dealing with a situation where I have 2 pages. The first page contains a div called 'ajaxicall', which loads another page inside it. The challenge I am facing is figuring out how to close the second page when I click on the "close" button w ...

Styling the Menu Item with CSS

A graphic designer provided me with these elements to incorporate into the HTML design. Everything was going smoothly until I encountered the faint, uneven borders on the LI tags, especially when dealing with a list containing only five items. If anyone ...

Animate the movement of a div

Within the given code snippet, the .logo element is initially hidden. The goal is to make it visible upon scrolling, while simultaneously animating the movement of the <ul> element to the right (e.g., sliding). Upon reviewing the demo provided, one c ...

Can you explain the distinction between importing a JavaScript module using these two methods?

As a beginner in javascript development and leveraging npm, I recently wanted to create uuids. In my search, I came across a uuid package that caught my interest: https://www.npmjs.com/package/uuid To utilize this package, I installed it by executing th ...

What is the best way to apply styles to a React component that is passed as a prop?

I'm currently working on a component that is passed as a label prop. In order for me to utilize justify-content: space between within my div, I must ensure it has a width of 100%. You can see how this appears in the developer tools by clicking here. r ...

AngularJS service for exchanging information among controllers

I am working on an AngularJS application (1.4.10) where I need to share data between two controllers. To achieve this, I created a factory: .factory('CardsForService', function($http, URL){ var service = { "block_id": '', ...

What is the reason for webpack searching for jQuery even though it is not necessary for the module?

Before I proceed with my question, I want to clarify that I may not fully grasp the intricacies of this issue and I apologize if it does not meet the standards of Stackoverflow's Q&A. Currently, we are facing a challenge in our project while tryi ...

Issue: React does not accept objects as valid children

I am facing an issue while trying to loop through the data fetched from the database in react. Here is the error I encountered: https://i.sstatic.net/WBSq4.png Even after rendering the list in a separate component, I am still unable to resolve the problem ...

Displaying JSP content within a <div> element with the help of JQuery

Here are the specifications for two important divs on my webpage. #apDiv1 { position:absolute; left:0px; top:115px; width:100%; height:100%; z-index:4; } #apDiv2 { position:relative; left ...

The process of dynamically inserting input types into a <td> element using the innerHTML tag is not functioning properly in Internet Explorer

I am facing an issue with dynamically placing input types within a <td> tag - the innerHTML() method is not functioning properly in Internet Explorer, although it works fine in Mozilla. This is how I am inserting the input types using JavaScript, wi ...