How can I change the visibility of certain elements by clicking a button on a website?

I have been working on a login and signup form that initially only asks for the username and password. Once the user clicks on the sign-in button, additional inputs such as first name, last name, date of birth, etc. should be displayed. I attempted to write code to achieve this functionality, but unfortunately, it is not working as expected.

#form{
  width: 300px;
  margin: 40px auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  border: 3px solid rgb(6, 61, 30);
  padding: 20px;
  border-radius: 30px;
  background-color: #e4e1ff;
}

.butts{
  display: flex;
  justify-content: center;
  position: relative;
}

#form .butt{
  width: 150px;
  height: 50px;
  font-size: 25px;
  display: inline-block;
  background-color: #e4e1ff;
  border: none;
}

hr{
  height: 4px;
  width: 150px;
  margin: 0;
  background: tomato;
  border: none;
  transition: 0.2s ease-in-out;
  position: absolute;
  left: 0px;
  bottom: 0px;
}

#butt1:focus ~ hr{
  left: 0;
}

#butt2:focus ~ hr{
  left: 50%;
}

#login_form{
  margin-top: 20px;
}

#login_form input{
  width: 300px;
  height: 50px;
  font-size: 20px;
  margin-bottom: 10px;
  border-radius: 20px;
}

#signup_form{
  display: none;
}

#butt2:focus ~ #signup_form{
  display: block;
}

#butt2:focus ~ #login_form{
  display: none;
}
<section id="form">
  <div class="butts">
    <button class="butt" id="butt1">Login</button>
    <button class="butt" id="butt2">Sign Up</button>
    <hr>
  </div>
  <div id="login_form">
    <input type="text" placeholder="username">
    <input type="password" placeholder="password">
  </div>
  <div id="signup_form">
    <input type="text" placeholder="First Name">
    <input type="text" placeholder="Last Name">
    <input type='date' placeholder="Birthdate">
    <input type='text' placeholder="username">
    <input type="password" placeholder="password">
    <input type="password" placeholder="confirm password">
    <button>Connect Github</button>
    <button>Create Account</button>
  </div>
</section>

Answer №1

function toggleForm(){
   var loginForm=document.getElementById('login_form');
   var signupForm=document.getElementById('signup_form');

   if(event.target.id == "button1"){
      loginForm.style.display='block';
      signupForm.style.display='none';
   }else{
       loginForm.style.display='none';
       signupForm.style.display='block'; 
   }
}
#form{
    width: 300px;
    margin: 40px auto;
    display: flex;
    flex-direction: column;
    justify-content: center;
    border: 3px solid rgb(6, 61, 30);
    padding: 20px;
    border-radius: 30px;
    background-color: #e4e1ff;
}

.buttons{
    display: flex;
    justify-content: center;
    position: relative;
}

#form .button{
    width: 150px;
    height: 50px;
    font-size: 25px;
    display: inline-block;
    background-color: #e4e1ff;
    border: none;

}


hr{
    height: 4px;
    width: 150px;
    margin: 0;
    background: tomato;
    border: none;
    transition: 0.2s ease-in-out;
    position: absolute;
    left: 0px;
    bottom: 0px;

}

#button1:focus ~ hr{
    left: 0;
}

#button2:focus ~ hr{
    left: 50%;
    
}

#login_form{
    margin-top: 20px;
}

#login_form input{
    width: 300px;
    height: 50px;
    font-size: 20px;
    margin-bottom: 10px;
    border-radius: 20px;
}

#signup_form{
    display: none;
}

#button2:focus ~ #signup_form{
    display: block;
}

#button2:focus ~ #login_form{
    display: none;
}
<section id="form">
    <div class="buttons">
        <button class="button" id="button1" onClick="toggleForm()">Login</button>
        <button class="button" id="button2" onClick="toggleForm()">Sign Up</button>
        <hr/>
    </div>
    <div id="login_form">
        <input type="text" placeholder="username">
        <input type="password" placeholder="password">
    </div>
    <div id="signup_form">
        <input type="text" placeholder="First Name">
        <input type="text" placeholder="Last Name">
        <input type='date' placeholder="Birthdate">
        <input type='text' placeholder="username">
        <input type="password" placeholder="password">
        <input type="password" placeholder="confirm password">
        <button>Connect Github</button>
        <button>Create Account</button>
    </div>
</section>

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

Wrapping text around an image using two distinct Angular components

Can text wrap around an image in Angular even if they are in separate components? Or do the text and image have to be within the same component for this to work, regardless of whether the image is on the left or right side? https://i.stack.imgur.com/hdlxD ...

How to retrieve XML data using the HTML method

This webpage is in XML format. To access the live status information, you can visit Since there is no server-side code available on this free hosting platform, you'll need to perform an HTML GET request to check if ls:islive is true or false. if ...

Using Python in Django to seamlessly add products to the shopping cart

Seeking guidance in creating a simple function that can capture the product ID and insert it into the user's shopping cart upon clicking the "add to cart" button. I have already set up the shopping cart table and the product table but am struggling to ...

Ensure that the height of the bootstrap image is limited by the dimensions of another column while still preserving its

I need to develop a versatile element with bootstrap that can accommodate images of different aspect ratios. To achieve this, I must trim the right side of the picture like so. <div class="container"> <div class="row"> <div class="col-12" ...

Just starting out with Flexbox - what steps should I take?

In the code, there are three divs arranged in the DOM by div-00#. I am seeking to achieve the following layout using flexbox (for width >= 460px) as shown in the images below: https://i.sstatic.net/I2PFQ.png Added: 18-12-16 - https://i.sstatic.net/v ...

What are the steps to developing a responsive tile using HTML and CSS?

I am exploring the functionalities of CSS and responsive design. To better understand how it works, I created a simple example which can be accessed here. This demonstration showcases a basic tile template. My goal is to display my tiles in 2, 3, 4, or m ...

Effects of jQuery Show / Hide on adjacent select box operations

I created a pair of select boxes where the visibility of one is controlled by the other. Initially, the controlled select box (#select02) works perfectly on page load as long as I don't show/hide it by selecting options in the controlling select box ( ...

disable the option to call on your iPhone

Hello there, I am currently working on a project which has a mobile browser version. I have noticed that on iPhone, all numbers are callable by default. My goal is to identify and select all callable numbers while disabling the call option. For i ...

Create a heading for the selection menu

I need to customize a dropdown list on my page so that the text "Select Language" is always displayed to the user, regardless of whether they make a selection from the dropdown. Even if the user chooses "Option 1" or "Option 2," the default text should sti ...

Submission form fails to go through or button for submitting is malfunctioning

After researching, I believed that this code would function properly. However, upon implementation, it is not working as expected. The main issue is that the form is failing to submit. How can I troubleshoot this problem? Furthermore, I am unable to re ...

Design of list items using card elements

Struggling with creating a card list design. I want to use MDB5 (utilizing Angular and the MDB5 package - ) but here's the concept: Imagine a white page where within a grey background, there is a list of item cards. Each card consists of an image on ...

The end of the /if in the small template system fails to correctly detect the desired condition

If I write the following line on its own, he cuts it without any reason. So...: Greetings, I'm in need of assistance regarding an issue that is currently beyond my understanding. I'm facing a challenge with a small TPL-System which requires so ...

Material UI causing animation to fail to trigger

After integrating material UI into my existing application, I encountered a peculiar issue. When adding material UI components to modals, the entering animation fails to trigger. Interestingly, downgrading material UI or removing all MUI components resolve ...

Storing website data for localization purposes on the web platform

I am currently working on a project to develop a website that displays the selected language page upon first visit. The idea is that when a user clicks on a language name, such as French, it will be stored in web/local storage. Then, when the user returns ...

Increasing and preserving the width value of a div using SCSS?

I am currently working on a div where I need to increase the width based on checkboxes that are selected. The goal is to add to the width as more checkboxes get checked. Here is an example of the HTML code I'm working with (please excuse any syntax e ...

The background hue does not cover the entire element

When I hover over my drop-down menu button, the grey color only covers up to where I set the padding. Here is my HTML snippet: Help ▾ Give Get Help Get Involved I want the grey color to expand fully across the "li" ...

Align elements vertically in flexbox container using Bootstrap 3 on Internet Explorer 10 and 11

Encountering a problem in IE10+11 when trying to vertically center two Bootstrap columns within a row using flexbox. The dynamic content in the columns requires the row to have a minimum height of 65px. However, if the content expands and spans multiple l ...

What is the process to transfer data from JavaScript to HTML within a Laravel environment?

I am attempting to transfer a value from JavaScript to HTML as a variable. In order to do this, I am retrieving the value from a Laravel PHP controller. JavaScript $("ul.nav-tabs > li > a").click(function() { var id = $(this).attr("href").repla ...

Result of mixing CSS colors

Is there a way to retrieve the result of blending two colors using the color-mix function? In cases where the color-mix feature is not supported, is it possible to set a fixed value instead? ...

Is it possible to utilize WebMatrix for hosting a website?

Is it possible to utilize Webmatrix for publishing and editing websites? I am currently using Weebly and getting tired of logging in and out to make changes. Can I edit my website with Webmatrix and publish it directly from there? ...