In order to activate form fields when a radio button is selected, and deactivate them when it is not selected

My form currently has 2 radio buttons, with one already checked by default. I've disabled some fields and want to enable them when the second radio button is checked, and disable them (as well as restore CSS properties) when the first radio button is checked.

Currently, I am only able to do this for input fields without using any scripts.

Any assistance on this matter would be greatly appreciated.

  input[disabled='disabled'], textarea[disabled='disabled'], select[disabled='disabled'] {
        background: #ddddbb;
        color: black;
        cursor: default;
    }

    input[type="text"]:disabled, input[type="select"]:disabled {
        background: #ddddbb;
    }
<html>
<title>Form</title>
<body bgcolor="#FFFFFF" topmargin="20" leftmargin="50">

    <input type="radio"
           name="radSize"
           id="generic_test"
           value="false" checked="checked" onclick="document.getElementById('project_id').disabled=true;" />
    <label for="generic_test">Generic Test</label>

    <input type="radio"
           name="radSize"
           id="project_test"
           value="true" onclick="document.getElementById('project_id').disabled=false;" /><label for="project_test">Project Test</label>
    <table align="left">
        <tr>
        <tr>
            <td width="126" height="24" align="left"
                valign="middle">
                <span class="f1">Project ID <span class="star">*</span></span>
            </td>
            <td width="126" height="24" align="left"
                valign="middle">
                <input type="text"
                       name="project_id" id="project_id" size="19" 
                       value="" disabled="disabled">
            </td>
        </tr>
        <tr>
            <td height="24" width="50" align="left"
                valign="middle">
                <span class="f1"> Multiphase <span class="star">*</span></span>
            </td>
            <td align="left" valign="middle" nowrap="nowrap">
                <select name="multiphase"
                        id="multiphase"
                        onchange="onChangeFunction();" disabled="disabled">

                    <option value='' selected></option>
                    <option value='Yes'>Yes</option>
                    <option value='No'>No</option>
                </select>
            </td>
        </tr>
        <div>
            <tr>
                <td class="f1">Text Area<br><span class="star">*</span>&nbsp;&nbsp;&nbsp;</td>
                <td>
                    <textarea id="textInputField3"
                              name="textInputField3"
                              style="resize:vertical; text-align:top;  "
                              rows="5"
                              cols="52"
                              disabled="disabled"></textarea>
                </td>
            </tr>
        </div>
        </tr>
    </table>

Answer №1

To make the css rule effective, utilize `removeAttribute` for the Attribute `disabled` to activate the field or use setAttribute to deactivate it. https://developer.mozilla.org/de/docs/Web/API/Element/removeAttribute https://developer.mozilla.org/de/docs/Web/API/Element/setAttribute

function enable(enabled){
  var project = document.getElementById('project_id'),
      multiphase = document.getElementById('multiphase');
      textInputField3 = document.getElementById('textInputField3');
  if(enabled){
    project.removeAttribute('disabled');
    multiphase.removeAttribute('disabled');
    textInputField3.removeAttribute('disabled');
  } else {
    project.setAttribute('disabled','disabled');
    multiphase.setAttribute('disabled','disabled');
    textInputField3.setAttribute('disabled','disabled');
  }
}
input, textarea, select {
        background: #fff;
        color: black;
        cursor: default;
    }
input[disabled='disabled'], textarea[disabled='disabled'], select[disabled='disabled'] {
        background: #ddddbb;
    }

    input[type="text"]:disabled, input[type="select"]:disabled {
        background: #ddddbb;
    }
<html>
<title>Form</title>
<body bgcolor="#FFFFFF" topmargin="20" leftmargin="50">

    <input type="radio"
           name="radSize"
           id="generic_test"
           value="false" checked="checked" onclick="enable(false)" />
    <label for="generic_test">Generic Test</label>

    <input type="radio"
           name="radSize"
           id="project_test"
           value="true" onclick="enable(true)" /><label for="project_test">Project Test</label>
    <table align="left">
        <tr>
        <tr>
            <td width="126" height="24" align="left"
                valign="middle">
                <span class="f1">Project ID <span class="star">*</span></span>
            </td>
            <td width="126" height="24" align="left"
                valign="middle">
                <input type="text"
                       name="project_id" id="project_id" size="19" 
                       value="" disabled="disabled">
            </td>
        </tr>
        <tr>
            <td height="24" width="50" align="left"
                valign="middle">
                <span class="f1"> Multiphase <span class="star">*</span></span>
            </td>
            <td align="left" valign="middle" nowrap="nowrap">
                <select name="multiphase"
                        id="multiphase"
                        onchange="onChangeFunction();" disabled="disabled">

                    <option value='' selected></option>
                    <option value='Yes'>Yes</option>
                    <option value='No'>No</option>
                </select>
            </td>
        </tr>
        <div>
            <tr>
                <td class="f1">Text Area<br><span class="star">*</span>&nbsp;&nbsp;&nbsp;</td>
                <td>
                    <textarea id="textInputField3"
                              name="textInputField3"
                              style="resize:vertical; text-align:top;  "
                              rows="5"
                              cols="52"
                              disabled="disabled"></textarea>
                </td>
            </tr>
        </div>
        </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

Transform Razor-generated HTML content into a downloadable PDF file

I am currently working with a dynamic filtered table containing different elements, each accompanied by a set of buttons for specific operations. One button displays a partial HTML generated using Razor that includes the element's data. For example, ...

The functionality of findDOMNode is no longer supported

My website, built using React, features a calendar that allows users to select a date and time range with the help of the react-advanced-datetimerange-picker library. However, I encounter some warnings in my index.js file due to the use of <React.Stric ...

What is the best way to assign a value to a Dropdown menu just once, even if the values are repeated

I am dynamically populating a dropdown with some data like this: if (product.oPTIONS.length > 0) { $.each(product.oPTIONS, function (idx, options) { /*appending the each option to a label*/ $("#productdetails").append('<div ...

Dynamic burger menu navigation

I am in the process of designing a full-page navigation overlay for my website to ensure consistency across all devices. Currently, I have two buttons - one to display the overlay and another to hide it. I am considering whether it would be more effective ...

Customize the appearance of a hyperlink

I am having difficulty styling a link's title using jQuery-mobile. Each link represents a player with unique abilities. When the user hovers over a player, I want a tooltip to display their special ability. Despite trying various code samples, none ha ...

Exploring Illumination with Three.js

I'm interested in exploring the light properties further. I am curious about the variables used in the DirectionalLight.js and SpotLight.js source codes. Could you explain the difference between castShadow and onlyShadow? Is there a way to manage th ...

The persistent issue with Vue.js is the constant occurrence of undefined problems

Hello, I am new to working with Vue and I am currently trying to utilize props in my component. However, I am facing an issue where the prop data is always undefined and showing as $attrs in the Vue-Debugger. My Plan: I intend to use the TabWrapper Compon ...

What methods can be used to identify the specific Router component being rendered in React?

I am currently working with a Navbar component that I want to render with different CSS styles using styled-components based on the route of the component being rendered. How can I determine whether that component is being rendered or not? const NavbarCont ...

The Angular component is failing to display the template

After following a tutorial on UI-Router () I have implemented the following states in my Angular application: angular .module('appRoutes', ["ui.router"]) .config(['$stateProvider', '$urlRouterProvider', function($sta ...

Choosing the vue js el by utilizing the CSS attribute selector: A step-by-step guide

Here is my situation: <div data-identifier='app'> ... </div> This is the Vue js code I am working with: var app = new Vue({ el: '[data-identifier]', data: { }, ...

A complete guide on executing synchronous HTTP requests while using async.each in Node.js

My goal is to send HTTP requests to APIs, retrieve data for each user, and then insert that data into MongoDB. The issue I'm facing is that all the requests are being made simultaneously, causing the process to get stuck at some point. I'm using ...

Ensuring JSON data protection when sending Ajax requests in JavaScript (for(;;);)

After extensive research, I have not been able to find the answer I'm looking for despite similar questions being asked. My query concerns the usage of for(;;); while(1); before an Ajax response outputs a JSON string. I am curious about how this tec ...

Encountering issues with displaying the JSON response in the table, receiving an undefined error

When I display the JSON response in a table, I am encountering an issue where it shows undefined. The data received can be either one record or multiple records from the database. Interestingly, the correct output is displayed in the alert box. The Control ...

Express app unable to retrieve dropdown menu data using Node.js BodyParser

I am currently working on creating a pug file for a signup form and I'm encountering an issue with the drop-down menu not functioning. How can I properly include my drop-down menu in the body parser? I initially assumed it would be processed with json ...

What causes Mocha to exit prematurely before a reporter finishes handling a test failure?

Currently, I am developing a test suite using PhantomJS (via selenium-webdriver) in conjunction with Mocha. In order to have screenshots generated whenever a test fails, I created a custom reporter for Mocha patterned after the spec reporter: module.expor ...

Node JS Axios Network Error due to CORS Policy Restrictions

When attempting to make a put axios request, I encounter the following error: https://i.sstatic.net/aBQGI.png I have installed and enabled the CORS module in the server.js file, but it doesn't seem to be working. Additionally, there are no CORS head ...

Constantly loading image with Meteor HTTP request

Within my Meteor application, I am attempting to dynamically load a random image from an API which returns JSON data structured like this: { "id":2026 "url": "https:// ... " , "large_url":null, "source_id":609, "copyright":"CC0", "site":"unsplash" } ...

My code is currently experiencing issues with jQuery

Here is the markup that I am using; <tbody> <tr> <td class="expand">Showroom area - New Display Zone</td> <td>300</td> <td>350</td> <td class="short ...

Is there a way for lengthy texts to automatically flow across multiple columns?

Is it possible to achieve a similar functionality in web design as that found in Microsoft Word with multiple columns? Specifically, I am looking for the text to automatically transition from column 1 to column 2 once the first column is filled. I have se ...

Javascript's ReferenceError occasionally acts inconsistently when using Firefox's scratchpad

While delving into the world of Javascript for learning purposes, I encountered an unexpected behavior. Let's explore this scenario: function hello(name) { let greet = 'Hello ' alert(greet + name) } hello('world') alert(gree ...