When selecting a specific option on a radio button in JSP, I aim to have a textbox appear on the screen

One of the features on my website is a radio button with 2 choices. Here's how it looks in the code:

<td>City : </td>
      <td><input type="radio" name="r_city" value="Same" checked="checked" />Same</td>
      <td><input type="radio" name="r_city" value="Different" />Different</td>
      <td>textbox here</td>

When "Different" is selected, I want to make a textbox appear on the page.

This functionality is part of a JSP page. Thank you!

Answer โ„–1

If you could share the JavaScript framework that you are using, it would be helpful.

When working with vanilla JavaScript, a common method is to toggle hidden classes on click events for better browser compatibility.

Check out this Plunker example for reference.

// JavaScript
window.onload = () => {
  var rad2 = document.getElementById('r2');
  var rad1 = document.getElementById('r1');
  var input = document.getElementById('myInput');
  rad2.addEventListener("click", function(){
    input.classList.remove('hidden');
  });
  rad1.addEventListener("click", function(){
    input.classList.add('hidden');
  });
}


// HTML
<h1>Hello Plunker!</h1>
<td>City : </td>
<td><input type="radio" name="r_city" id="r1" value="Same" checked="checked" /></td>
<td><input type="radio" name="r_city" id="r2" value="Different" /></td>
<td><input id="myInput" class="hidden"/></td>

// CSS
.hidden {
    display: none;
}

Answer โ„–2

Here is the solution, simply utilize the .hide() function to hide it again

<table>
  <tr>
    <td>City : </td>
    <td><input type="radio" name="r_city" value="Same" checked="checked"/>Same</td>
    <td><input type="radio" id="testChecked" name="r_city" value="Different" />Different</td>
    <td class="show-this" style="display:none">textbox here</td>
  </tr>
</table>

<script>
    $('#testChecked').click(function(){
      if ($(this).is(':checked'))
      {
        $('.show-this').show();
      }
  });
</script>

Don't forget to include jQuery***

Answer โ„–3

Here's a helpful tip for you! If the textboxes inside <td> have a common class, try using the class selector instead of input[type="text"].

$(document).ready(function(){
  $("input[type='radio']").click(function(){
    var textBox = $(this).closest('tr').find('td input[type="text"]');
    if($(this).val() === 'Different'){
      $(textBox).show();
    } else {
      $(textBox).hide();
    }
  });

});
td input[type='text'] {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
      <td>City : </td>
      <td><input type="radio" name="r_city" value="Same" checked="checked" />Same</td>
      <td><input type="radio" name="r_city" value="Different" />Different</td>
      <td><input type='text' /></td>
  </tr>
    <tr>
      <td>City : </td>
      <td><input type="radio" name="r_city1" value="Same" checked="checked" />Same</td>
      <td><input type="radio" name="r_city1" value="Different" />Different</td>
      <td><input type='text' /></td>
  </tr>
</table>

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

How can I use a button to save all changes made in a JqGrid checkbox column simultaneously?

In my jqgrid, there is a column named 'asistencia' which displays data from a database as checkboxes. These checkboxes are pre-checked based on the property formatoptions: {disabled : false}. I would like to implement a 'save' button th ...

Issue with AngularJS $http not responding to ng-click after first call

My landing controller uses a service that initiates the $http call by default when the App loads. However, I need to pass parameters based on click events, so I implemented an ajax call on ng-click. The issue is that I keep receiving the same data on ng-c ...

The error message "TypeError: jQuery(...).ready(...) is not a function" indicates that

Although this question has been asked before, none of the answers seem to solve my specific issue. I am attempting to run a small piece of jQuery code (as I am just starting to learn it). jQuery(document).ready(function(){ jQuery('.comtrig') ...

Combining String Elements in Multidimensional Array with JavaScript

I currently have a 2D array filled with student profiles. In the first column, we have First Names, in the second Middle Names, and in the third Last Names, continuing on to other columns. My objective is to merge the text from all three columns, convert ...

Using jQuery functions on inputs within a Bootstrap Modal

I've encountered an issue with my jQuery functions that validate input fields based on a regex pattern. Everything works smoothly when the form is displayed on a regular page, but as soon as I try to implement it within a Bootstrap Modal, the validati ...

How can you create an event that is focused on options using Material-UI's Autocomplete feature?

This is regarding Material-UI's Autocomplete feature. I am looking for a way to track which autocomplete choice the user is currently focused on, whether it be through hovering over with the mouse or using keyboard arrows - before any actual selection ...

When using the require() function in Node.js, the period "." is not being recognized as part of the command and

I recently encountered a problem while working on my project and reorganizing my files. I noticed that the "." in my requires are not being parsed correctly. Upon running my program, an error message is displayed: Error: Module './src/map/createMa ...

npm Error: The installation script for [email protected] has encountered a failure

I encountered an error while attempting to execute "ionic start MyApp" in the Windows terminal. This is a new machine and I have already reinstalled nodejs and ionic, but the same error persists. Can someone lend a hand? npm WARN tar invalid entry & ...

The utilization of 'new' is not allowed with an expression that does not have a call or construct signature in TypeScript

While searching for a solution, I stumbled upon this link which unfortunately did not provide the answer I was looking for: Cannot use 'new' with an expression whose type lacks a call or construct signature I am facing a similar issue. In my JavaS ...

Utilize $or or $and for incorporating multiple conditions in a match statement

I've seen this question posted before, but I'm having trouble locating the answer. I want to filter shows with flag 1 and flag 2 using $or for searching. How can I include a condition for flag 2 along with flag 1 in my code? Currently, I only ha ...

The socket.io-client could not be located on the running Node.js server

Setting up a fresh installation of Node, Express, and Socket.io on a Linux environment using npm. When attempting to run a sample from the official socket.io source, I encountered an error stating that the 'socket.io-client' module was not found ...

Retrieve the previous URL for redirection purposes

I have a specific route set up in my application. If the user is not logged in, they are redirected to the login page. I am currently working on capturing the previous route that the user came from so that I can redirect them back there after they have suc ...

Adjust Scale to Less than 1 in JVectorMap

I am in the process of developing a website that includes a full-size map using JVectorMap. The map currently occupies 100% of the width and height of the page, but I would like to add a border around it when fully zoomed out. However, when the map is zoom ...

Error encountered when attempting to send JSON data with special characters via a POST or PUT request using node.js's http.request()

While attempting to use the node.js http module to PUT a JSON data into CouchDB, I encountered an issue. The JSON included a special character "รค" which caused CouchDB to respond with an "invalid_json" error. However, once the special character was remove ...

How to retrieve the current event handler's value with jQuery

To assign an onclick event handler using jQuery, I simply use the following code: $('#id').click(function(){ console.log('click!'); }); Now, my question is how can I retrieve a reference to the function that is currently handling th ...

Retrieve the music player data using getElementById

Currently, I am in the process of constructing a music player using the HTML5 audio functions API. Before diving into coding the audio functionality of the player, I want to incorporate getElementbyID and onClick functions to exhibit results in separate di ...

Using Pug/Jade, be sure to call a function during each loop iteration

I'm attempting to develop a basic app, similar to a TO-DO LIST. I want to generate divs dynamically (with incremental ids) when a Button is clicked and then enter some text into an HTML input field. For example: <div id="item1"> <div id="ite ...

Determine whether the object is facing the specified position

I'm attempting to verify whether an object (this.target) is facing towards a particular position (newPosition). Here's what I currently have: new THREE.Matrix4().lookAt( newPosition, this.target.position, this.target.up ) == this.target.matrix ...

The functionality of Dnd-kit nested is functioning properly, however, one specific component is unfortunately

Currently, I am dealing with a two-dimensional array that results in two nested dnd-kit drag and drop fields. The nested dnd functions correctly; however, the first one sorts items when moved without any animations (even when moving my div). Below is the ...

Ensure that Heroku is using a designated version of Node.js

My node.js application runs perfectly fine on my local machine, but I encounter an error when I deploy it to Heroku: 2012-04-11T00:42:55+00:00 app[web.1]: throw e; // process.nextTick error, or 'error' event on first tick 2012-04-11T00:4 ...