Ways to display distinct text boxes based on selected radio button?

Currently, I am working with JSP and have implemented an HTML form that includes a "Process" button at the top. When this button is clicked, it displays a form with two radio buttons - TestClient and TestServer.

The form also contains a Submit button.

If you want to view my work, here is the link to my jsfiddle

Issue Description:-

My current goal is to display two text boxes and one radio button below the selected radio button (TestClient or TestServer) once it is clicked. Here is what I want it to look like:

TestClient      TestServer

Name    textbox
Id      textbox
Sex     Male    Female

Submit button

Similarly, if TestServer is clicked, I would like to show three text boxes and a dropdown menu below the respective radio button.

TestClient      TestServer

Address         textbox
ClientId        textbox
ServerId        textbox
Country         dropdownbox

Submit button

Is it achievable using jQuery in my existing jsfiddle example? If so, could someone provide me with a helpful jsfiddle demonstration?

Answer №1

Totally feasible. I have included a simple demonstration using div elements for each radio button with customized content inside them. Feel free to personalize the inputs as needed.

Initially, conceal these divs using CSS.

<div class="user" style="display: none">
    User details go here
</div>

<div class="admin" style="display: none">
    Admin details go here
</div>

You can implement a logic similar to this in your jQuery function:

$('input[name="user"]').click(function() {
    if($(this).attr('id') == 'user') {
        $('.user').show();
        $('.admin').hide();
    } else {
        $('.user').hide();
        $('.admin').show();
    }
});

Answer №2

If you'd rather not write the HTML code directly and prefer to generate it using jQuery, here's an alternative solution:

Check out this JSFiddle link

<button id="primary">Process</button>
<form method='post'>
  <h4>Process</h4>
    <fieldset><legend>process</legend>
      <input  id="client" value="TestClient"type="radio" name="cli_srv">
      <label for="client">TestClient</label>
      <input  id="server" value="TestServer" type="radio" name="cli_srv">
      <label for="server">TestServer</label>
    </fieldset>
    <fieldset id="form_process">
    </fieldset>
</form>
    <button form="form_process">Submit</button>

Additionally,

$("#primary").click(function(){
    $("form").show()
});

var form= $('#form_process');

$("#client").click(function(){
    form.html(null);
    form.append('<label for="cli_name">First Name</label><input value="John" id="cli_name"><br>')
    form.append('<label for="cli_id">ID Number</label><input value="12345" id="cli_id"><br>')
// etc...
})

$("#server").click(function(){
    form.html(null);
    form.append('<label for="srv_address">Server Address</label><input value="123 Street" id="srv_address"><br>')
    form.append('<label for="srv_id">Server ID</label><input value="56789" id="srv_id"><br>')
// etc...
})

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 make placeholder text automatically adjust its size to fit within the text input element and display its complete content?

I am working on a piece of HTML code that includes a form with a table for traveler information. However, I am having trouble ensuring that the placeholder text remains fully visible at all times. To give you a visual example, here is what I currently hav ...

Delete elements with identical values from array "a" and then delete the element at the same index in array "b" as the one removed from array "a"

Currently, I am facing an issue while plotting a temperature chart as I have two arrays: a, which consists of registered temperature values throughout the day. For example: a=[22.1, 23.4, 21.7,...]; and b, containing the corresponding timestamps for eac ...

Avoid running another process before the current one finishes in jQuery

I have a situation where I am using $.ajax inside a for loop in jQuery. for(var i=0; i < 2; i++) { $.ajax({ url :"/models/getdata"+i, dataType:"json", success:function(data) { } }); } The issue is that before the success function for i=0 completes, ...

Assistance needed for iterating through JSON data

I need assistance retrieving specific data from a JSON response in my JavaScript code. Unfortunately, the current approach is not providing the desired results: This is my JavaScript code snippet: function retrieveArtists(response){ var artistList ...

Bootstrap Navigation Bar Animation causing Input Fields to be cut off

I am working with a collapsible bootstrap NavBar that contains two elements within the collapsible <div>, <form class="form-inline> and <select class="custom-select" id="menu"> (Refer to my code below) The problem I'm encountering ...

Creating an array of objects by parsing JSON using the jQuery .each() method

I am attempting to generate an array of objects by parsing a JSON file. Here is the pertinent code: //president object constructor function president(a_presName, a_presDates, a_presNick, a_presImage) { this.presName=a_presName; this.presDates=a_pr ...

Steps for returning a res.send(req.body) upon sending back to a function

I'm currently working on sending the req.body from a POST request route back to the executing function for further processing. The structure of req.body is as follows: { username: 'aa', password: 'ss' } After making the post requ ...

Error occurred while fetching image from Medium story API in Next.js

While working on my Next.js app, I encountered an issue with the Medium story API. Every time I try to fetch and display an image using the API, I receive an error message stating "upstream image response failed." The specific error code is: upstream image ...

Executing memory function with JQuery replaceWith() and event handlers

Imagine you have the following HTML code: <button id="click-me" type="button">Click Me!</button> Now, picture this jQuery snippet being executed: var button = $('#click-me'); button.on('click', function() { console.l ...

What methods can be used to decrease the width of a space?

I currently have this on my website: https://i.sstatic.net/lfBum.png But I want to organize the 3 elements to look like this: https://i.sstatic.net/SwyMs.png <th class="fc-day-header fc-widget-header fc-sat" data-date="2016-10-01">DAY 1<br> ...

Using the https module in Node.js to transfer a file to a PHP server

What is the best method to send an HTTP post request that includes a jpg file to a php server using the node https module? I attempted to use the request module, but it is unreliable (timing out most of the time) and already deprecated. Here is the functi ...

Update the table in a few moments

I am struggling to refresh my table every 5 seconds but haven't been successful. <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example"> <thead> <tr> <th>Number</th> ...

What are the steps to recursively transform a JavaScript object?

I am currently working on recursively transforming a JavaScript object, but I have encountered an issue. The problem is: if any object key has exactly two properties - 'label' and 'value', then the value should change to the label only ...

Issue with Jquery Mobile: Radio Buttons becoming disabled following a sorting action

I need to present 3 questions in random order, each with a single choice: Q1, Q2, and Q3. Upon sorting them using DIVs, all radio buttons are then disabled. Below is the HTML markup and JavaScript code snippet: ............................................. ...

What are the steps to making a textfield editable with JavaScript and HTML?

My textfield is currently populated with values from the database and is functioning perfectly. I would like to implement a checkbox feature that, when clicked, will clear the textfield of the database values, allowing the user to input new values. If t ...

Conditional return type mistakes

I'm facing an issue with a function that takes a parameter "value" and is supposed to return 0 or 1 based on its true or false value. Check it out here. const f = <T extends boolean>(value: T): false extends T ? 0 : 1 => { if (value === ...

Discover the power of lodash's .groupBy method as you learn how to efficiently group objects within another

Utilizing lodash's _.groupBy function, I have generated the following data: { "Generic Drugs":[ { itemDes: "Dulcolax", itemGeneric: "Bisacodyl", pr ...

If the URL matches a specific path, then append a parameter

I've created a script that adds a parameter to a URL based on specific subfolders. For example, if the URL contains /de, it will add ?_sft_language=german. However, I'm encountering an issue where the code is running multiple times instead of jus ...

Is there a way in AngularJS to set all radio buttons to false when one is chosen as true?

When I retrieve true/false string values from the database, I am unable to alter the data. The example I provided is simply a representation of the string values true or false that I receive from the database. My goal is to have a single radio button disp ...

What's the best way to prevent extra spacing when typing in a materialUI TextField?

Instructions for the user: Please input your first name without any spaces between characters. I attempted to implement the following code, however, it did not achieve the desired outcome. <TextField required id="name" label="First Name" ...