How to trigger a JavaScript function by clicking on a hyperlink in an HTML document

How do I style my divs so that when I click on sign up, the sign in div hides and vice versa? I have tried using this JS code, but both divs appear simultaneously on the window. How can I modify the code to achieve the desired behavior of hiding one div while showing the other?

$(document).ready(function() {
  $("#signIn").click(function() {
    $(".signUpHide").hide(600);
    $(".signInShow").show(600);
    $(this).hide();
    $("#signUp").show();
  });
});

$(document).ready(function() {
  $("#signUp").click(function() {
    $(".signInHide").hide(600);
    $(".signUpShow").show(600);
    $(this).hide();
    $("#signIn").show();
  });
});
body {
  height: 100vh;
  width: 208vh;
}

.leftPanel {
  background-color: #660033;
  width: 70%;
  height: 100%;
  float: left;
  position: absolute;
}

.rightPanel {
  background-color: white;
  width: 30%;
  height: 100%;
  float: right;
}

.signInClass {}

.signInHide {}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="rightPanel">
  <div class="container">
    <div class="row" style="margin-top: 30px">
      <label class="col-md-3"></label>
    </div>

    <div class="signUpHide signUpShow">
      <div class="row" style="margin-top: 30px">
        <label class="col-md-2"></label>
        <h1 class="col-md-10"><a name="SignUp">Sign Up</a></h1>
        <label class="col-md-2"></label>
        <label>or <a href="#SignIn">sign in</a> to your account</label>
      </div>

      <form action="bbSignUp.php" method="post">

        <div class="row" style="margin-top: 20px">
          <label class="col-md-2"></label>
          <input type="text" class="form-control col-md-8" placeholder="Username" name="user-name">
          <label class="col-md-2"></label>
        </div>

        // More form elements...

      </form>
    </div>

    <div class="signInShow signInHide">

      <div class="row" style="margin-top: 30px">
        <label class="col-md-2"></label>
        <h1 class="col-md-10"><a name="SignIn">Sign In</a></h1>
        <label class="col-md-2"></label>
        <label>or <a href="#SignUp">create an account</a></label>
      </div>

      <form action="bbSignIn.php" method="post">

        // Form elements...

      </form>
    </div>
  </div>
</div>

Answer №1

To enhance the code, consider consolidating it into a single `document.ready` function and leverage the `toggle()` method for toggling between displaying and hiding elements. Additionally, ensure to rectify the usage of `id` by opting for `id="SignIn"` instead of `href="#SignIn`.

$(document).ready(function() {
  $("#signIn,#signUp").click(function() {
    $(".signIn, .signUp").toggle();
  });
});
height: 100vh;
width: 208vh;

}
.leftPanel {
  background-color: #660033;
  width: 70%;
  height: 100%;
  float: left;
  position: absolute;
}
.rightPanel {
  background-color: white;
  width: 30%;
  height: 100%;
  float: right;
}
.signInClass {}
.signInHide {}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

<div class="rightPanel">
  <div class="container">
    <div class="row" style="margin-top: 30px">
      <label class="col-md-3"></label>
    </div>

    <div class="signUp" style="display:none;">
      <div class="row" style="margin-top: 30px">
        <label class="col-md-2"></label>
        <h1 class="col-md-10"><a name="SignUp">Sign Up</a></h1>
        <label class="col-md-2"></label>
        <label>or <a href="#" id="signUp">sign in</a> to your account</label>
      </div>

      <form action="bbSignUp.php" method="post">

        <div class="row" style="margin-top: 20px">
          <label class="col-md-2"></label>
          <input type="text" class="form-control col-md-8" placeholder="Username" name="user-name">
          <label class="col-md-2"></label>
        </div>>

        [...] (content continues)
</div>
</div>
</p>
    </div></answer1>
<exanswer1><div class="answer accepted" i="59079718" l="4.0" c="1574885999" m="1574898248" v="3" a="TmlkaGluIEpvc2VwaA==" ai="3289660">
<p>To elevate the quality of the code, start by condensing it into a single `document.ready` block and utilize the `toggle()` method to switch between displaying and hiding elements. Furthermore, address the incorrect implementation of `id` by replacing `href="#SignIn"` with `id="SignIn`.</p>

<p><div>
<div>
<pre class="lang-js"><code>$(document).ready(function() {
  $("#signIn,#signUp").click(function() {
    $(".signIn, .signUp").toggle();
  });
});
height: 100vh;
width: 208vh;

}
.leftPanel {
  background-color: #660033;
  width: 70%;
  height: 100%;
  float: left;
  position: absolute;
}
.rightPanel {
  background-color: white;
  width: 30%;
  height: 100%;
  float: right;
}
.signInClass {}
.signInHide {}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

<div class="rightPanel">
  <div class="container">
    <div class="row" style="margin-top: 30px">
      <label class="col-md-3"></label>
    </div>

    <div class="signUp" style="display:none;">
      <div class="row" style="margin-top: 30px">
        <label class="col-md-2"></label>
        <h1 class="col-md-10"><a name="SignUp">Sign Up</a></h1>
        <label class="col-md-2"></label>
        <label>or <a href="#" id="signUp">sign in</a> to your account</label>
      </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

In Vue.js, utilize a contenteditable div to dynamically add an HTML element and bind it with v-model

Below is the HTML code I have written. <div id="app"> <button @click="renderHtml">Click to append html</button> <div class="flex"> <div class="message" @input="fakeVmodel" v-html="html" contenteditable="true">< ...

Display text using HTML characters within an HTML table

Is there a way to easily print strings with HTML characters in a readable format, like '<sometext>'? Also, can we replace <br /> with spaces? $description = str_replace("%q", " < ", str_replace("%w", " &g ...

What is the best method for checking for JavaScript errors when using Selenium WebDriver in conjunction with NodeJS?

When it comes to coding in JavaScript, not Java, my focus is on running a specific website like foo.com. I typically set it up as follows: var webdriver = require("selenium-webdriver"); By = webdriver.By, until = webdriver.until; var chrome = req ...

Integrate the user input data into a modal window using Bootstrap

Looking for a solution to a problem, I'm trying to implement a system on my website that sends messages to registered users. After creating a form with necessary information, clicking the send button should display a preview of the message before send ...

How come my website is loading a JavaScript file with outdated content?

After making edits to a JavaScript file that replaces specific HTML tags with new language translations, I noticed that the changes were not reflected correctly on the website. Even after clearing my history and cache, Firebug still showed the old content ...

Is it possible to apply a CSS transform without erasing the existing transform?

Looking for a solution: elem transform translateY(5px) scale(1.2) I want the element to move down an extra 5px on hover elem:hover transform translateY(5px) Is there a way to achieve this without knowing the previous state of the transform? Appr ...

Enhancing jQuery UI popups with custom styles and layout

Currently, I am working on a web application that relies heavily on jQuery. To create popup windows, I have integrated jQuery UI dialog along with jQuery UI tabs and jScrollPane for customized scroll bars. The overall structure of the application is as fol ...

"Creating Interactive Slider Bullets: A Guide to Linking them to Their Respective

I can't seem to figure out how to connect my slider bullets to my slider slides. I want bullet 1 to correspond to slide 1, bullet 2 to slide 2, and so on. It should be a simple task, but I'm struggling to make it work. I have set up a basic reset ...

Looping through JSON data in jQuery outputs the value 9 when paired with the text() method

I have retrieved JSON data that is structured as follows: { "solution": { "1": { "cell-1": "1", "cell-2": "2", "cell-3": "3", "cell-4": "4", "cell-5": "5", "cell-6": "6", ...

What consequences could occur in HTML if the main tag is not properly enclosed within the body tag?

Just starting out. While working with HTML, I experimented with placing the main tag outside of the body tag instead of nesting it inside as recommended by HTML5 standards. To my eye, both versions looked identical when I viewed the HTML file side by sid ...

What is the process for creating a button that can sort an array and count its elements?

I am having trouble creating a code that can sort products into alphabetical order and then count the number of items in the list within the same program. I have managed to write separate programs that achieve each of these tasks individually, but when I ...

What is the best way to show an array of elements that have been passed as a prop to a functional

After creating the amenitiesArray with elements like club_house, swimming_pool, etc., I attempted to map over it using .map(). However, when I passed it as a prop, I encountered an issue with displaying it on the screen. Any ideas why? The output of conso ...

Transport parameter via routerLink to a different component

When I click on the link provided in the list-profile HTML document, I need to send a variable. However, the current code is not working and causing crashes. If more details are required, feel free to ask. List Profile Component HTML <div class="col c ...

Hover Effect in JavaScript

After thoroughly checking all the duplicates below, I still couldn't quite figure out how to achieve the desired effect. Here are some resources I looked into: make animation hover like transition hover JS hover-like animation Hover animation with js ...

Removing a specific item from a Kendo UI dropdown list

Recently, I encountered a predicament with a dropdownlist populated from a datasource. Following a certain event, my goal is to eliminate a single item from the dropdownlist identified by id = 22. Although I recognize this may not be the best practice du ...

Sending colored output from stdout to grunt's output stream

Having trouble creating a custom grunt task that runs mocha tests and displays colored output? I'm running into an issue where grunt is stripping out the colors from the mocha command's output. Here's the code for the grunt task: var exec = ...

Tips for updating data in vue.js

I am currently facing a challenge where I need to use vue.js to edit and update values. While I have successfully implemented the edit functionality to retrieve the values, I am encountering difficulties with updating them. To trigger the update action, ...

How can I code a script to import JSON data into MongoDB database?

I have a JSON file named "data.json" that contains an array of people's names as shown below: "data": [ { { "name":"John", "age":30, } { "name":"Mark", "age":45, } } ] I am ...

Retrieving JSON Data from an API with JavaScript/jQuery

Having some trouble with JSON parsing using jQuery and JavaScript. I'm trying to fetch weather information from the open weather simplest API, but for some reason it's not working. When I hardcode the JSON data into a file or my script, everythin ...

Changing the z-index property of a Material-UI <Select> dropdown: What you need to know

Currently, I am implementing an <AppBar> with a significantly high z-index value (using withStyles, it is set to theme.zIndex.modal + 2 which results in 1202). The primary purpose behind this decision is to guarantee that my <Drawer> component ...