Tips for updating information when a button is chosen

Hello everyone, I need some help with a form that has three select buttons. The button labeled "Distribute" is already selected when the page loads, and it contains information about full name, password, and location. How can I use JavaScript to create a click effect where selecting any button will deselect the pre-selected one and change the content accordingly? To illustrate further, here are some images:

Initial Page: https://i.sstatic.net/fIJCx.png

Expected Outcome after selecting "Buy": https://i.sstatic.net/zhaRo.png

Here's the code snippet:

(CSS code here)
(HTML code here)

To summarize, I want each button click to show different content based on the provided images. Your assistance is greatly appreciated. Thanks!

Answer №1

Enclose every form in its own individual div and display only the first one by default. Give each button a unique ID and link these IDs to the corresponding divs containing the forms. On clicking each button, reveal the associated div while concealing all other sibling divs.

Answer №2

You can check out the snippet below. Please note that some HTML has been removed from your example, but I have managed to fulfill the request "I want to be able to click all buttons and each button clicked should have different content as seen from the images."

In the snippet below, I used JavaScript to achieve this along with some updates on the form. Keep in mind that you can achieve this using jQuery or other methods, this is just one approach.

Here is my approach:

  • I added a new attribute to the Button [for] and Form [id] to connect them. This adds clarity by indicating which button is intended for which form.
  • After that, you just need to write a few lines of JavaScript.
  1. Create a function to switch forms.
  2. Use JavaScript to add a click event to the button so that when clicked, it calls the switch forms function.
  3. Using JavaScript, you will remove the "active" class from any div that has both the classes "app-button" and "active", and then add the class to the current caller.
  4. By utilizing the "for" attribute of those buttons, we can find the corresponding form to hide and show.

This code may not be fully optimized and lacks animation. You could enhance it by adding CSS transitions, toggling classes, implementing timers, or simply incorporating jQuery with the Fade function for simplicity.

(function() {
  const switchForm = (element) => {
    const oldActive = document.querySelector(".app-button.active");

    oldActive.classList.remove("active");
    document.querySelector(`#${oldActive.getAttribute("for")}`).style.display = "none";

    element.classList.add("active")
    document.querySelector(`#${element.getAttribute("for")}`).style.display = "";
  }

  document.querySelectorAll(".app-button").forEach(btn => {
    btn.addEventListener("click", () => switchForm(btn));
  });
})()
* {
  box-sizing: border-box;
  list-style: none;
  text-decoration: none;
  padding: 0;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  display: flex;
  font-size: 16px;
  user-select: none;
}


/* APPLICATION */

.applicant {
  width: 60%;
  margin: auto;
  text-align: left;
  margin: 90px auto;
  padding-top: 100px;
  padding-bottom: 100px;
}

.login-center {
  height: 100%;
  flex-basis: 41%;
  background: #ffffff;
  padding: 35px 5px 0px 5px;
  box-sizing: border-box;
  box-shadow: 0 0 50px 0px #e1e1e1;
}

.text h2 {
  text-align: center;
  font-size: 35px;
  font-weight: 600;
  color: #000;
}

.links {
  text-align: center;
  margin-left: -20px;
  margin-right: -20px;
  margin: 0 auto;
  padding-top: 50px;
}

.app-button {
  color: #c4c4c4;
  background-color: #ffffff;
  border: 1px solid #000;
  font-weight: bold;
  font-size: 17px;
  width: 200px;
  margin: 0 20px;
  display: inline-block;
  line-height: 40px;
  padding: 5px;
  cursor: pointer;
}

.app-button:hover {
  background: #c4c4c4;
  color: #000000;
  transition: 0.5s;
}


/* ACTIVE STATE */

.links .active,
.app-button:hover {
  border: 1px solid #c4c4c4;
  background-color: #c4c4c4;
  color: #000;
}


/* FORM */

.form input::placeholder {
  font-size: 14px;
  color: #000;
}

.form label {
  color: #000;
  margin: 20px 0;
  font-size: 17px;
}

.form-u {
  margin: 70px 0;
  padding: 0px 100px;
}

.form input {
  width: 100%;
  padding: 20px;
  margin: 20px 0;
  box-sizing: border-box;
  border: none;
  outline: none;
  background: #ffffff;
  border: 1.7px solid #e1e1e1;
}

.form input:focus {
  border-color: #c4c4c4;
  box-shadow: 0 0 15px 0px #e1e1e1;
}

.button-u {
  color: #c4c4c4;
  background-color: #000;
  border: 1px solid rg#c4c4c4;
  font-weight: bold;
  font-size: 17px;
  width: 100%;
  margin: 40px 0;
  display: inline-block;
  line-height: 40px;
  padding: 5px;
  cursor: pointer;
}


/* DROPDOWN FOR LOCATION*/

.form input {
  font-size: 15px;
  color: #000;
}

.selector {
  width: 100%;
  padding-top: 20px;
  margin-bottom: 25px;
  position: relative;
}

#selectField p {
  font-size: 15px;
}

#selectField {
  width: 100%;
  background: #ffffff;
  box-sizing: border-box;
  border: 1px solid #c4c4c4;

  padding: 20px 20px;
  color: #000;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  position: relative;
}
<!--Form-->
<section class="applicant">
  <div class="login-center">
    <div class="text">
      <h2>CREATE</h2>
    </div>
    <div class="links">
      <button type="button" for="distribute" class="app-button active">Distribute</button>
      <button type="button" for="buy" class="app-button">Buy</button>
      <button type="button" for="sell" class="app-button">Sell</button>

    </div>
    <div class="form-u">
      <form action="" class="form" id="distribute">
        <label for="uname"><b>Distribute Inputs:</b></label>
        <input type="text" name="name=" Enter full name "required">
        <button type="submit " class="button-u ">SUBMIT DISTRIBUTE</button>
      </form>
      <form action="" class="form" style="display:none;" id="buy">
        <label for="uname"><b>Buy Inputs:</b></label>
        <input type="text" name="name=" Enter full name "required">
        <button type="submit " class="button-u ">SUBMIT BUY</button>
      </form>
      <form action="" class="form" style="display:none;" id="sell">
        <label for="uname"><b>Sell Inputs:</b></label>
        <input type="text" name="name=" Enter full name "required">
        <button type="submit " class="button-u ">SUBMIT SELL</button>
      </form>
    </div>

  </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

Can other JavaScript event listeners be synchronized with an asynchronous event?

My goal is to manipulate a web page using Tampermonkey where there is a button with certain event listeners followed by a redirect. Is it possible for me to insert my own click event handler into the button, which triggers an AJAX request and waits for it ...

JavaScript for automatic file reading

I'm currently working on a personal file storage website as part of a practice project. The code itself is functional, but I'm facing the challenge of managing a large JSON file containing the names of all the files I want to display on the websi ...

Tabbed horizontal slider

I am trying to combine two scripts in order to create a tab-based system with a scrollbar located at the bottom of the content. I have merged the following Ajax tabs: with this slider: However, when I open the slider's tab, I am unable to move the s ...

Storing form information within the view

What is the best way to transfer data between views in AngularJS? I tried using $rootScope but it's not working as expected. ...

showing divs in a compact layout on smaller screens using Bootstrap 5

Is there a way to set up two div elements so that they are displayed as blocks on small screens and inline on large screens? I also need their width to span 100% of the screen. ...

Incorporating a PHP file containing both PHP and JavaScript variables into an AJAX document

I'm facing an issue with my application that involves a language file called "lang.php", along with "index.php" and "ajax.php" files. The "lang.php" file contains both PHP and JavaScript variables which I include in both "index.php" and "ajax.php" to ...

Determine the percentage of clicks on an HTML5 canvas radial progress bar

Recently, I designed a circular progress bar displaying a specific percentage. However, I am facing a challenge in determining how to calculate the percentage when a user clicks on either the black or green part of the circle. Could you provide insight on ...

Custom Line-height Mixin for Styling Efficiency

I'm currently utilizing a certain mixin to create font sizes in rem with fallback pixel values, while also determining a line-height that is 1.5 times the font size. .font(@size: 16px, @line: @size) { @remfont: (@size / 10); @remline: (@size / 10) * ...

How to render specific components in Next.js instead of rendering the entire page using React

Currently, I am exploring how to utilize next.js to display a component within a layout upon clicking a link. The layout structure I have is as follows: import * as React from "react" import { Box } from "@chakra-ui/layout" import { L ...

Is it feasible to choose the component generated by this element?

My current dilemma involves a component that renders a form, however, it also has its own form "catcher". var FormUpload = React.createClass({ submit : function(){ var formdata =new FormData(); ...

Problem with Jquery Ajax, failing to recognize option values

Hello everyone, please review the code snippet below... $.fn.myajax = function (options) { var defaults = { my_event: "", my_url: "", my_data: "", } var o = {}; var mydata = options.my_data; $.extend(o, default ...

Customizing screen dimensions for a single page using jQuery mobile

I am currently facing an issue with adjusting the size of a specific page within my application. Even though I am using <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, target-den ...

Pressing the follow button does not save data in the database

Whenever I click on a follow button, it should store followerid and followingid in the follow table and change to an unfollow button. If I then click on the unfollow button, those entries should be deleted from the follow table. I have used JQuery for this ...

Solving the issue of image paths within external SCSS files in Nuxt.js

Trying to organize my component scss files separately from their Vue components has been a bit challenging. I also have a GLOBAL scss file that is not scoped, but no matter which approach I take, I can't seem to get the image paths in /assets or /stat ...

Have you attempted to configure a JSON file to facilitate language translations?

I need some guidance on structuring my data.json file efficiently. Currently, it is set up as shown in the example below. The goal is to have a drop-down menu where users can select a language and then display 50 different pages with specific content. I wa ...

Failed to fetch http://localhost:8000/Stack/script.js due to network error 404 in Django project

As I embark on creating a simple to-do app, I encountered an error while trying to implement some JavaScript on the homepage. The error in question is highlighted above (Get http://localhost:8000/Stack/script.js net:: Err_Aborted 404). Being new to integra ...

I'm running into an issue with my React component where it is rendering before the state is set, causing it to fail. Is there a

Is it possible for me to achieve what I've been attempting for the past week? I feel like I'm so close, yet still can't quite reach my goal. Could it be that what I'm trying to do is impossible? I am using reactjs and next js (although ...

Interactive Div that Adapts

Hello everyone, I'm new to this forum and seeking some assistance. I have a requirement where multiple div contents need to fade in and out dynamically. I found this jsfiddle example that works for 2 divs, but I want it to work for more, say 5 differ ...

The reflow feature in Highcharts does not properly update the width of the container for High

https://i.sstatic.net/8Shix.gif I am currently working on adjusting the width of Highcharts dynamically to fit its parent container. To achieve this, I have set up a mechanism that listens to changes in the sidebar state and triggers a reflow function whe ...

How can I line up elements in different divs when the width is adjusting based on the window size?

Trying to align a search input and a result element in separate containers when the window size changes. Looking for a solution without using Javascript. One window size: A smaller window: Currently utilizing but facing challenges with responsive desig ...