What is the best way to toggle between two forms with JavaScript?

I have designed a login screen with two forms inside one div. The first form contains the login box, while the second form is for registration and is hidden using CSS with display:none;. Below the login button, there is a paragraph with an anchor tag to click for registration. How can I make it so that when you click the anchor tag, it switches from the login form to the register form?

I am at the very beginning stage when it comes to JavaScript.

<div class="login-page">
        <div class="form">
            <form class="register-form">
                <input type="text" placeholder="login"/>
                <input type="password" placeholder="password"/>
                <input type="text" placeholder="e-mail"/>
                <button>create</button>
                <p class="message">Already have an account? <a href="#">Sign in!</a></p>
            </form>
            <form class="login-form">
                <input type="text" placeholder="login"/>
                    <input type="password" placeholder="password"/>
                    <button>Sign in</button>
                    <p class="message">Need an account? <a href="#">Register!</a></p>
            </form>
        </div>
    </div>

I found this code on CodePen but I'm having trouble getting it to work on my website:

<script> $('.message a').click(function(){$('form').animate({height: "toggle", opacity: "toggle"}, "slow");}); </script>

Answer №1

If your script isn't working, one potential reason could be that it is placed within the <head>...</head> section of the document instead of right before the closing </body> tag.

In such a case, the script may fail because the <body>...</body> hasn't finished loading when the script runs. Make sure to check for any errors in your browser's console.

Another possibility is that you loaded jQuery after your custom script, like this:

<script>...</script>
<script src="code.jquery.com/jquery-3.3.1.min.js"></script>

Instead, it should be loaded in the correct order, like this:

<script src="code.jquery.com/jquery-3.3.1.min.js"></script>
<script>...</script>

Answer №2

Use some custom styling to conceal the login form.

$('.message a').click(
  function() {
    $('form').animate({height: "toggle", opacity: "toggle"}, "slow");
  }
 );
.login-form {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="login-page">
    <div class="form">
        <form class="register-form">
            <input type="text" placeholder="login"/>
            <input type="password" placeholder="password"/>
            <input type="text" placeholder="e-mail"/>
            <button>create</button>
            <p class="message">Already have an account? <a href="#">Sign in!</a></p>
        </form>
        <form class="login-form">
            <input type="text" placeholder="login"/>
                <input type="password" placeholder="password"/>
                <button>Sign in</button>
                <p class="message">Need an account? <a href="#">Register!</a></p>
        </form>
    </div>
</div>

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

Is there a way to display the product name in the input field when it is selected using the keyboard?

One of our team members has created an angular js script for autocomplete search functionality. Typing keywords in the search bar will return a list of candidates. However, when using arrow keys to navigate through the candidates, the product id is displ ...

What advantages come from selectively importing a single function from a Node.js package on the backend, rather than importing the entire package?

Consider a scenario where I only require the ObjectId function from the mongoose package in my file. Are there any advantages (in terms of CPU usage, memory consumption, speed, etc.) to importing just that specific function instead of the entire mongoose ...

Implementing RXJS subscription using a Subject object

What is the benefit of using "Subscribe providing subject" and what does this entail: 1- The purpose of using subscribe providing subject import { Subject, from } from 'rxjs'; const newSubject = new Subject<number>(); newSubject.subscr ...

Efficiently storing multiple file fields in Django with the help of jQuery and Ajax

I need assistance with saving multiple file fields to a server. The fields are dynamic, allowing users to upload as many files as they want using an add button. I prefer not to use form submit or dropzone. Can someone please advise me on how to achieve thi ...

Adjust the cursor style using Javascript

I am currently working on a paint software program and I have a feature where pressing the ctrl key while moving the mouse turns on the eraser. However, when the key is pressed, I want the cursor (currently a crosshair) to change to none. I have tried impl ...

Exploring the concept of Javascript Objects and the undefined value

I've encountered an issue with a Wordpress plugin that is no longer supported. After some investigation, I have identified the problem area in the code: i[C][0]; The above line seems to be causing an 'undefined' return within the function ...

Defeat the all-powerful !important tag and enable dialog customization for positioning

I am currently developing a browser extension and I am encountering an issue with using JQueryUI to display a modal dialog. The problem arises when certain websites, for example espn.com, also utilize JQueryUI but do not properly scope their own elements. ...

The error message "Cannot read property 'addEventListener' of undefined" occurred while trying to register the service worker using `navigator.serviceWorker.register('worker.js')`

I'm grappling with a JavaScript issue and need some help. You can find the demo of the functioning script by the author right here. I've implemented the same code as displayed on his demo page. I've downloaded the worker.js file and ...

The outcome of the AJAX RSS Reader remains unpredictable

Utilizing the AJAX RSS Reader (visit this link) for extracting RSS data from my URL: http://vtv.vn/trong-nuoc.rss In my php file (readRSS.php): <?php $url = ("http://vtv.vn/trong-nuoc.rss"); $xmlDoc = new DOMDocument(); $xmlDoc->load($url); $ ...

``How can I apply styling to a child component in React within a regular HTML element?

I'm currently learning React and faced a unique situation while working on projects. Here's the scenario - I have two React child components nested under a standard HTML element. If I need to target and style each individual React child element ...

Is there a way to apply a blur effect to just the div itself without affecting its contents

<style type="text/css"> .inner-container{ width:400px; height:400px; position:absolute; top:calc(50vh - 200px); left:calc(50vw - 200px); overflow:hidden; } #no-blur{ z-index: 20; } .box{ position:absolute; height: ...

Obtain the month, day, or year from a timestamp

Can anyone provide guidance on extracting a specific date format (date, month, day, year) from a timestamp? I am interested in implementing this feature within a view using Backbone JS. ...

Is there a way to limit the height of the tbody element?

I need to set a max-height rule for a table's tbody with overflow set to auto, so that a scrollbar only appears when the table exceeds a certain height. Is there a way to restrict the height of a tbody element within a table in this manner? The tabl ...

Retrieving JavaScript return values in a C# WebBrowser control within a WPF application

I utilized JavaScript injection into WebBrowser control in C# (System.Windows.Controls.WebBrowser) to achieve, <C#> IHTMLDocument2 webdoc = (IHTMLDocument2)webBrowser1.Document; string var = File.ReadAllText("C:/.../Resources/script.txt"); object re ...

The wrapper is failing to extend the full height of the entire page

Initially, I thought setting the height of my wrapping div to 100% would be a quick task that I could complete in five minutes. However, it ended up taking me hours, so now I'm here hoping someone can assist me with this commonly asked question. The ...

Having trouble getting the .push() method in JavaScript to function correctly

I've encountered an issue while trying to develop a function that iterates through each item in an array and records the index of a specific item when found. In this particular function, whenever the item 'contraband' is detected during the ...

Unable to retrieve data from file input using jQuery due to undefined property "get(0).files"

I am facing an issue with retrieving file data in jQuery AJAX call from a file input on an ASP.NET view page when clicking a button. HTML <table> <td style="text-align:left"> <input type="file" id="AttachmenteUploadFile" name="Attachme ...

Issue with onclick event not being triggered by Javascript function inside constructor

My current challenge involves implementing a function called makeHighlight within the SmartButton constructor. The purpose of this function is to execute whenever I click on the SmartButton object, which is represented by an image element. To achieve thi ...

Error: Unable to locate module '@material/core/Grid'

After cloning a repository that is built with MUI, I noticed that certain components were already imported and working seamlessly. These components include: import Card from '@mui/material/Card' import CardActions from '@mui/material/CardAct ...

Unlocking Component Variables Beyond the Bounds - Vuejs

Suppose I have a compound called <myCompound>, how can I access the ref or variables from the outside? For example: myCompound.vue : <script setup> import { ref } from "vue"; const myString = ref("hi string"); </script&g ...