Instructions for deactivating a drop-down list using radio buttons in JavaScript

I am working on a project where I have 3 radio buttons and 3 different combo boxes in my HTML. I want to use JavaScript to enable/disable the combo boxes based on which radio button is selected. For example, when the G1 radio button is clicked, I want only the G1 combo box to be enabled and the others to be disabled. How can I achieve this functionality with JS?

<input type="radio" name="g1" value="g1"> G1
<input type="radio" name="g2" value="g2"> G2
<input type="radio" name="g3" value="g3"> G3


<select name="g1">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>

<select name="g2">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>

<select name="g3">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>

Answer №1

Check out this jQuery code snippet that works perfectly:

<script type="text/javascript" language="javascript">
    $(document).ready(function(){
        $('.checkbox').click(function(){
            $('.checkbox').not(this).removeAttr("checked");
            var id="select_"+$(this).attr('id');
            $('select').removeAttr("disabled");
            $('select').not("#"+id).attr('disabled','true');
        });
    });
</script>

HTML:

<input type="checkbox" name="group1" value="group1" class="checkbox" id="group1"> Group 1
<input type="checkbox" name="group2" value="group2" class="checkbox" id="group2"> Group 2
<input type="checkbox" name="group3" value="group3" class="checkbox" id="group3"> Group 3


    <select name="group1" id="select_group1">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    </select>

    <select name="group2" id="select_group2">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    </select>

    <select name="group3" id="select_group3">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    </select>

JSFIDDLE: *http://jsfiddle.net/Yzabm/*

Answer №2

Give this a shot:

http://jsfiddle.net/VeaVh/1/

There are more efficient selectors available, but this serves as a simple example.

Answer №3

Make sure to include an ID in your dropdown field so that you can interact with it using JavaScript.

By assigning an ID, you will be able to disable the dropdown field as needed.

Utilize the `onclick` event of a radio button to trigger a script that enables or disables the dropdown field.

document.getElementById('your_select_id').disabled = true;

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

Streaming large files with Node.js can lead to significant memory consumption and potential memory errors like OOM

My current project involves using node.js to download large files (300MB) from a server and then piping the response to a file write stream. While I have a good understanding of how pipes work in Node.js, I am encountering an issue where the memory usage o ...

Tips for calculating the total of a virtual column through child associations in Sequelize

I have a network of 3 interconnected objects (A, B, and C). There can be multiple Bs associated with A, and multiple Cs associated with each B. For instance: A └───B │ └───C │ └───C └───B └───C Object ...

Having trouble loading the Google API using getScript. Is displaying a ReferenceError message: "Variable google not found."

I am attempting to dynamically load the Google API using the getScript() method for implementing a "Place Autocomplete Address Form". For more information, you can visit this link: https://developers.google.com/maps/documentation/javascript/examples/places ...

Search for the location where the code is not functioning as intended

Take a look at this HTML code snippet: <input value="9961" name="c_id" id="c_id" type="hidden"> <input name="user_id" id="user_id" value="1" type="hidden"> <textarea id="comments" name="comments" style="width: 310px; resize: none; height: 7 ...

Utilizing JSON data in JavaScript to populate a datatable

After receiving a JSON result from an AJAX jQuery call, I stored it in a variable and attempted to insert the JSON data into a datatable. However, instead of inserting the data from the variable as intended, the datatable is currently populated with JSON d ...

Error: The variable "message" has not been defined. Please define it before displaying the welcome

While I was experimenting with my welcome message and attempting to convert it into an embed, I ended up rewriting the entire code to make it compatible. However, upon completion, I encountered the error message is not defined. var welcomePath = './ ...

The digest string for the crypto.pbkdf2Sync function is malfunctioning

I've been working on revamping the authentication system for an old application that previously ran on node 4.5, but I keep encountering an error whenever I attempt to log in. TypeError [ERR_INVALID_ARG_TYPE]: The "digest" argument must be one of type ...

What is the reason behind Meteor automatically updating a record without the need to run a "Meteor.call" function for an update?

I am currently developing a Meteor.js application and while I have grasped the basic concepts of Meteor, I feel like I might be missing something when it comes to its reactivity principles. Using angular-meteor, I am utilizing a $scope variable in my view ...

The Node.js Express server does not provide access to certain static files

I am currently utilizing the angularjs-gulp-browserify-boilerplate for my development environment on Windows 10. Once I run gulp in dev mode, static files are moved to the build directory: ./build |_js |_css |_img |_fonts |_lang In additio ...

Ways to turn off emojis in froala editor

I successfully integrated the Froala editor and dynamically generated an ID for it based on specific requirements. Everything is working well, but I now need to disable emoticons in this editor. $('#froala'+key).froalaEditor({ imageUploadP ...

Javascript Code. Can you explain what this means: ", (err) => { }"?

I'm a bit puzzled as to how to describe this, so I'm struggling to articulate my question clearly. Can someone please explain what the operator is and what it is accomplishing in this code snippet? My assumption is that it's passing a functi ...

Sort arrays in Javascript by their respective lengths

Is there a way to arrange these arrays in descending order of the number of items they contain? I believe the logic is correct, but I might be missing some essential methods. The current output is empty. //Declare Variables var TN = ['Chattanooga&apo ...

Having trouble with the jQuery load function not functioning properly

I have encountered an issue with this code snippet while running it on XAMPP. The alert function is working fine, but the HTML content fails to load. I have included links to jQuery 3.2.1 for reference. index.html $(document).ready(function(){ $("#b ...

Create dynamic text on an HTML canvas starting from the center, displaying one character at a time while constantly adapting to the formatting

I am currently in the process of developing a website that has the capability to animate text, drawing each character onto a canvas one by one. I have successfully completed the initial coding for this functionality. However, my next challenge involves cen ...

There was a SyntaxError that caught me by surprise in my Javascript code when it unexpectedly encountered

I have been encountering an error in my code consistently, and I am struggling to comprehend why this is happening. The problematic area seems to be in line 29 of my Javascript file. HTML <link href="Styles12.css"; type="text/css" rel="stylesheet"> ...

How to align an unordered list vertically in the center using Bootstrap?

Trying to figure out why the ul within this parent div is not centered vertically. Here is a screenshot of the issue: https://i.sstatic.net/ZOc9M.png <div className=" d-flex align-items-center bg-primary "> <ul ...

Setting a background image in vanilla-extract/css is a straightforward process that can instantly enhance

I am brand new to using vanilla-extract/CSS and I have a rather straightforward question. I am attempting to apply a background image to the body of my HTML using vanilla-extract, but I am encountering issues as I keep getting a "failed to compile" error. ...

Instead of returning an object, the underscore groupBy function now returns an array

Currently, I am attempting to utilize underscore to create an array of entities that are grouped by their respective locations. The current format of the array consists of pairs in this structure { location: Location, data: T}[]. However, I aim to rearran ...

Whenever I try to open my jQuery UI Dialog without first displaying an alert, it does not work

Beginning of my HTML page, initializing a dialog : <script type="text/javascript"> $(function() { $( "#dialog-confirm" ).dialog({ autoOpen: false, resizable: true, height:180, width:300, modal: true, buttons: { "Yes": ...

Exploring the differences between detecting the XMLHttpRequest object in JavaScript and using the try

When it comes to determining browser support for AJAX, I typically rely on object detection like this: if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } ...