Implement a toggle feature for login and registration with jQuery

I am facing an issue with the show/hide model on my website. When I click the link associated with it, nothing happens. I want it to function in a way that when I click "Sign up," the register form is displayed and when I click "Login," the register form is hidden and the login form is shown. Below is the code I have written. HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Login Page</title>
        <link rel="stylesheet" href="style1.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="showhide.js"></script>
    </head>
    <body>
        <div class="login-page">
            <div class="form">
                <form class="register-form">
                    <input type="text" placeholder="Name:">
                    <input type="password" placeholder="Password:">
                    <input type="email" placeholder="Email:">
                    <button>Create</button>
                    <p class="message">Already Registered? <a href='#'>Login</a></p>
                </form>

                <form class="login-form">
                    <input type="text" placeholder="Username">
                    <input type="password" placeholder="Password">
                    <button>Login</button>
                    <p class="message">Not Registered? <a href="#">Sign up</a></p>
                </form>
            </div>
        </div>
    </body>
</html>

JS:

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

Your help with this problem is greatly appreciated.

Answer №1

e.target refers to the specific element that triggered an event, while .parent() method identifies its parent - in this case, the form element, and .siblings() method retrieves all siblings of the element.

$('.message a').click((e)=>{
   $(e.target).parent().parent().hide();
   $(e.target).parent().parent().siblings().show()
})

P.S To incorporate a fade effect into the hide/show methods, specify the time (in milliseconds) as an argument. For example: hide(1000)/show(2000)

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

Sort through an array using various prefixes and suffixes as filtering conditions

I have come across various questions and answers related to this particular issue, but most of them involve using the includes or indexOf methods. The problem at hand is how to filter an array (names in this scenario) using two different arrays - one with ...

AngularJS - enable user authentication to access exclusive content on the webpage without redirecting to a new page

I'm struggling with implementing a login form on my AngularJS site... My goal is to dynamically load a login form onto any page of the site without needing to navigate away, then asynchronously handle user login information and display a success or f ...

personalized XMLHttpRequest method open

var open = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function(method, uri, async, user, pass) { this.addEventListener("readystatechange", function(event) { if(this.readyState == 4){ var self = this; var respons ...

What is the best way to display a collection of images in an HTML page?

I have a list of strings containing images. How can I display these images on a webpage using Angular? images: [ "https://images.oyoroomscdn.com/uploads/hotel_image/1097/340ea5ee01acc37f.jpg", "https://images.oyoroomscdn.com/uploads/hotel_image/1097 ...

InvalidAction: The function forEach cannot be applied to "res"

Here is the HTML code that I am currently working with: <div *ngIf="chart" class="col-xl-4 col-lg-6"> <div class="card cardColor mb-3"> <div class="card-header headColor"> <img class="img-fluid" src="../../../ ...

Error thrown: SyntaxError - Forbidden break statement in AJAX code execution

Trying to exit a loop nested within a statement has been a challenge. Despite researching similar questions on stackoverflow, I have not found a solution that works. Below is the current code in question: for (var i = 0; (i < 10); i++) { ...

The issue arises with getInitialProps as it fails to pass data to the page component while attempting to retrieve initial information and subsequently modify it using a button

I am currently working on a component located at app\page.tsx in Next.js v13.4.12, and it includes a button. My goal is to have the button trigger the handleClick function when clicked. The issue I'm facing is that the getInitialProps function ...

How can I enable the "edit" function for a button within a list item?

I am currently working on a feature that allows users to edit list rows by clicking a button. However, I am facing an issue where the "edit" button only edits the first row instead of the matched line. This functionality is part of a program I am developi ...

The Challenge of the Universe's Origin

While watching the latest episode of The Big Bang Theory (Season 11, Episode 20), I found myself intrigued by Dr. Wolcott's unusual encryption method. In a quirky twist, this nutty theoretical cosmologist wrote his notes backward and converted all let ...

If the browser is Internet Explorer, then load file [A]; otherwise, load file [B]

One of my webpages requires unique content to be loaded for different web browsers. For example: If the browser is Internet Explorer {include file="/content1.tpl"} Else if it's any other browser {include file="/content2.tpl"} {/if} Content1. ...

The AJAX request fails to execute once the webpage has finished loading for the first time

Hey there! I have a button group where the selected button triggers an ajax call. Check out the code below: <div class="btn-group" id="graphSelection"> <button type="button" class="btn disabled btn-info" id="post" onclick="graphSelec ...

Making a dynamic accordion using JavaScript

I am currently coding in Javascript, without utilizing jQuery. Progress so far on the accordion title panel: var accordion = document.createElement('div'); accordion.className = 'panel-group'; accordion.id = 'accordion'; doc ...

Automatically refresh a pair of pages in sequence

Seeking advice on how to execute the following steps in JavaScript: - Pause for 120 seconds - Access http://192.168.1.1/internet-disconnect (in an iframe or similar) - Pause for 3 seconds - Access http://192.168.1.1/internet-connect - Repeat Here is ...

What are the steps to adjust the size of a browser window in WebDriverJS?

Currently, I am utilizing WebDriverJS, the JavaScript bindings for WebDriver, to conduct basic frontend testing (powered by nodejs). Nevertheless, encountering challenges resizing the window and the documentation available appears somewhat unclear in my u ...

Typescript declaration specifies the return type of function properties

I am currently working on fixing the Typescript declaration for youtube-dl-exec. This library has a default export that is a function with properties. Essentially, the default export returns a promise, but alternatively, you can use the exec() method which ...

Every time Fetch() is called in Node.js, a fresh Express session is established

Here is a snippet of code from a webshop server that includes two APIs: ./login for logging in and ./products to display products. The products will only be displayed after a successful login. The server uses TypeScript with Node.js and Express, along wit ...

Get ready for some Angular validation appearing on the screen

Using AngularJS, I have set up routes and validations on the HTML page. Here is a snippet of my HTML code: <form ng-controller="validateCtrl" name="loginForm" novalidate> <p>UserName:<br> <input type="text" name=" ...

Aligning a Material UI button to the far right of a row

I am struggling to align a React material-ui button to the right-most of the page and despite trying to use the justify="flex-end" attribute within the following code, it doesn't seem to be working: <Grid item justify="flex-end" ...

Dropdown menu not updating after second Ajax request

Recently, I've run into a problem that I could use some assistance with. I wrote a jQuery script to update an existing dropdown menu (disabling or enabling options based on returned results). It seems to be functioning correctly, but only on the init ...

Display visual information without requiring the parameters to be filtered beforehand in vue.js

Upon opening my page, I encountered an issue where the graphics appear blank. This is because I set up the callback for generating graphic data through params request. I wish to first fetch the general data when the page opens, and only load with params w ...