Using radio buttons to alter CSS properties of a submit button

I’m struggling to figure out how to change the css class of a submit button when toggling between two radio buttons. I've searched for examples but haven't found one that fits my needs. Can anyone offer some assistance?

Below is my form:

<form class="orderform">
    <input type="radio" name="choice" id="out" value="140" checked> <label for="out">I want A</label>
    <input type="radio" name="choice" id="in" value="40"> <label for="in">I want B</label>
    <input type="submit" value="Save changes" class="submit-btn-disabled" disabled="disabled">
</form>

The default appearance of the submit button should be as shown above (class="submit-btn-disabled"). When enabled, it should display with the class "submit-btn", along with a hover effect:

<input type="submit" value=" Save changes" class="submit-btn">

Here are the corresponding CSS classes:

input.submit-btn-disabled{
    -webkit-box-shadow: none;     
    -webkit-box-shadow: none;   
    box-shadow: none;
    -webkit-appearance: textfield;
    -webkit-box-sizing: content-box;
    -webkit-border-radius: 0em;
    -moz-border-radius: 0em;
    border-radius: 0em;
    border: none;
    background-color: #c4daaa;
    padding: 0.8em;
    font-size: 0.8em;
    color: #fff;
    cursor: pointer;
    margin-top: 1em;
}     

input.submit-btn{
    -webkit-box-shadow: none;     
    -webkit-box-shadow: none;   
    box-shadow: none;
    -webkit-appearance: textfield;
    -webkit-box-sizing: content-box;
    -webkit-border-radius: 0em;
    -moz-border-radius: 0em;
    border-radius: 0em;
    border: none;
    background-color: #3A7B30;
    padding: 0.8em;
    font-size: 0.8em;
    color: #fff;
    cursor: pointer;
    margin-top: 1em;
} 

input.submit-btn:hover{
    background-color: #82bc00;
}

Answer №1

Here is a handy solution - http://jsfiddle.net/jpatel/FXp8E/

You can achieve this by using the toggleClass function and making CSS changes.

JavaScript:

$("[name=choice]").click(function(){
            $('.submit-btn').toggleClass('active');
    });

CSS:

input.submit-btn.active{
background-color: #82bc00;}

Instead of changing the classes of submit-btn and submit-btn-disabled, you can simply add or remove the active class for the desired behavior.

Answer №2

To achieve the desired functionality, you may need to associate a click event with the radio buttons and then incorporate toggleClass and hasClass methods to adjust the appearance of the submit button and its disabled status. Here's an example:

$("#radio-buttons").on("click", function (e) {
    var isDisabled = $(":submit").hasClass('disabled');
    if(isDisabled) {
     $(":submit").toggleClass('disabled').toggleClass('enabled');
     $(":submit").prop('disabled', false);
    }
});

Answer №3

Hooray! After days of searching high and low, I finally stumbled upon the perfect solution right here. It only took me about 2 hours to solve the issue, and now I'm overjoyed! :) By combining some of the suggestions provided, I managed to add

«<action=”” method="post">
to the form. The solution appears to be working smoothly for me; fingers crossed that it's compatible with responsive websites and cross-browser functionality as well.


    <script type='text/javascript'> 
$(window).load(function(){
$(document).ready(function() {
    $('#out').click(function() {
        $('#submit').addClass('submit-btn-disabled');
        $('#submit').removeClass('submit-btn');
        $("#submit").prop('disabled', true);
    });
    $('#in').click(function() {
        $('#submit').removeClass('submit-btn-disabled');
        $('#submit').addClass('submit-btn');
        $("#submit").prop('disabled', false);
    });
});
});//]]>  
</script>

This solution is blazing fast for me! :)

Answer №4

I didn't follow the recommendation to use toggleClass(), so this code could be optimized further, but I believe I successfully addressed your objective.

Take a look at the following code snippet:

HTML (I simply included an id for the button):

<form class="order-form">
   <input type="radio" name="choice" id="out" value="140" checked /> <label for="out">I prefer Option A</label>
   <input type="radio" name="choice" id="in" value="40" /> <label for="in">I prefer Option B</label><br />
   <input type="submit" value="Save Changes" id='submit' class="disabled-submit-btn" disabled="disabled" /><br />
</form>

The CSS remains unchanged

and here is the jQuery section:

$(document).ready(function() {
    $('#out').click(function() {
        $('#submit').addClass('disabled-submit-btn');
        $('#submit').removeClass('active-submit-btn');
    });
    $('#in').click(function() {
        $('#submit').removeClass('disabled-submit-btn');
        $('#submit').addClass('active-submit-btn');
    });
});

You can also view it here: http://jsfiddle.net/dbf17/

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

Managing a multidimensional array in AJAX using PHP (specifically CodeIgniter) and JSON

Would like to display data from a database using Ajax in this HTML code. <div id="specContWrap"> <div id="overViewWrap" class="overViewTableCon"> <div class="overViewTable on"> <div class="tit">Overview</div> ...

Executing an Amplifyjs GET request containing a request body

Is it possible to utilize GET requests with a message body using AmplifyJS? Specifically, I am curious about the process of achieving this functionality with AmplifyJS. While synthetic tests function properly (using Fiddler as my test client), I have enc ...

Static checks do not apply to CSS variables

Our webtech class assignment is to create a static webpage that meets the criteria of the jigsaw w3c css validator. We've encountered a roadblock while trying to resolve the final warning message: Due to their dynamic nature, CSS variables are curre ...

What is the reason for the object being detected as undefined?

In an attempt to retrieve location coordinates within this function getLocation(){ $.ajax({ url:"//freegeoip.net/json/?callback=?", dataType:'json', success:function (data) { this.setState({ ...

The specified type '{}' cannot be assigned to type 'ReactNode'

Can someone help me figure out why I am getting this error in Vercel but not locally? Error: 'FontAwesomeIcon' cannot be used as a JSX component. ./components/Services/ServiceContainer.tsx:25:6 12:01:54.967 Type error: 'FontAwesomeIcon&a ...

What are the capabilities of an INNER JOIN query in Objection JS?

Here is the data model that I am working with: https://i.stack.imgur.com/g1T5i.jpg In this model, a User can be associated with multiple Projects, and a Project can have many Users. These relationships are managed through a join table called UserProjects. ...

Is there a reason why using margin:auto doesn't work for vertically centering items?

I'm having trouble understanding how margin auto functions. In the scenario I'm dealing with, I have a parent div and a child div nested within it, both with specified widths and heights. I attempted to use margin auto in order to center the inne ...

The functionality of "#button_1" remains unchanged despite using .click() or .hover() methods

Seems like I made a beginner's error - the #button_1 ID seems to be immune to the jQuery effects of click() or hover(). If someone could spare a moment to check out my JSFiddle, it would be incredibly helpful. It's likely something quite simple ...

The placeholder tags in the input box are not aligning correctly with Bootstrap 3.2

Can someone help me figure out how to make the placeholders in my input box for the dates fit properly? I've been trying to adjust the bootstrap code but haven't been successful. I have attached a screenshot of the issue. Below is the code snip ...

Could different sets of data be shown in different display modes using fullcalendar.js?

Is there a method to showcase distinct information based on the mode in which fullcalendar is being viewed? For instance, when in monthly mode, can the title appear as 'short title' with descriptions shown as tooltips? And when in weekly mode, c ...

Troubleshooting drag-and-drop functionality in a JavaScript HTML5 application resembling a Gmail upload interface

Here is a snapshot of my user interface: Each node in the tree structure is represented by an <li> element along with an <a> link. Furthermore, each folder serves as a dropzone for file uploads similar to the drag-and-drop feature found in Gm ...

Tips for implementing a custom filter in an AngularJS JavaScript controller

Can anyone help me with creating a custom filter in an AngularJS JavaScript controller? I need to be able to search for items in an array called segments by their SegmentId. The filter should iterate through the segments array and return the segment that ...

Optimizing Image Loading: Using jQuery and JavaScript to Load Images On Demand

Similar to the functionality on this site: I am interested in implementing a feature that only loads images when they are visible on the screen on my own website. Thank you. If possible, I would also like to add a preloader using jQuery. ...

"Streamlining data entry with an uncomplicated HTML form input that accepts multiple values from a

As I understand it, a barcode scanner functions as nothing more than a keyboard that transmits keycode 13 after each scan. My task is straightforward: I have a basic form with only one input field and I want the ability to scan numerous barcodes and then c ...

Implementing a method to pass total value to the <td> tag in angular JS using controller

I am having trouble calculating the total IR for each table cell. Despite my efforts, the function is not working as expected and I can't figure out why. $scope.getTotalb = function () { var totalb = 0; for (var i = 0; i < $scope ...

How to eliminate a particular validator from a form group in Angular

My goal is to eliminate the specific validator from the validator array so that I can reconfigure the controls when certain values have changed. I am aware of the traditional solution where I would need to repeatedly set validators. checked(event: MatC ...

Ways to activate the built-in HTML5 form validation without actually submitting the form

I have a form in my application that I plan to submit using AJAX. However, I would like to implement HTML5 for client-side validation. Is it possible to trigger form validation without actually submitting the form? Here is an example of the HTML code: &l ...

Having trouble with CSS values not being applied to dynamically injected HTML div elements in Angular 4?

Link to Codepen My Angular calendar application runs smoothly without any errors. However, I am encountering an issue where the CSS styles are not being applied to the page. When I implemented this separately, everything worked fine. But as soon as I inc ...

Node server receiving duplicate emit events from React socket io client

I have been working on a react application with node as the backend, and they are communicating through socket-io. The issue I am facing is that when React uses emit to send data to node, it sends the data twice, resulting in duplicate submissions. For ins ...

Using the Proper 'this' Reference Without Repeating 'this' in Nested Functions

I am facing an issue in my class where I have multiple methods and properties. One of these methods contains a setTimeout() function as follows: function myClass() { this.some_property = "test"; this.PrintOnTimeout = function() { // I thou ...