Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6169131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:48:15+00:00 2026-05-23T22:48:15+00:00

My form elements are generated by Rails. <input id=agree_to_rules name=agree_to_rules type=checkbox value=1> I’m trying

  • 0

My form elements are generated by Rails.

<input id="agree_to_rules" name="agree_to_rules" type="checkbox" value="1">

I’m trying this to set the state of the submit button on the form, based on whether the checkbox is checked or not. (I know inline css is not greate, I’m just trying to get a visual cue while I test.

<script>
$(function() {

  if ($('#agree_to_rules').is(':checked')) {
          $('#tweet_your_entry').attr('disabled', 'false').css({color: '#666666'});
              } else {
          $('#tweet_your_entry').attr('disabled', 'true').css({color: '#f5f5f5'});
        }
});
</script>

It just seems to always evaluate to false, no matter the state of the checkbox. Anyone spot the error? Or can point me to a cleaner way to achieve the same result — I’m very new to JS and jQuery.

UPDATE

Here’s what I’m using now, based on the answer below:

<script>
$(function() {
  $('#agree_to_rules').change(function() {
  if ( $('#agree_to_rules').prop("checked") ) {
          $('#tweet_your_entry').attr('disabled', 'false').removeClass('ui-button-disabled ui-state-disabled');
              } else {
          $('#tweet_your_entry').attr('disabled', 'true').addClass('ui-button-disabled ui-state-disabled');
        }
    });
});
</script>

The trouble is, when the button is active, after someone’s checked the box, the jquery-ui hover state for the button doesn’t kick in any more. Anyone know why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:48:17+00:00Added an answer on May 23, 2026 at 10:48 pm

    You need to place your code in a handler like .change(), and pass boolean true/false instead of strings.

    $(function() {
        $('#agree_to_rules').change(function() {
            if (this.checked) {
                $('#tweet_your_entry').attr('disabled', false).css({
                    color: '#666666'
                });
            } else {
                $('#tweet_your_entry').attr('disabled', true).css({
                    color: '#f5f5f5'
                });
            }
        });
    });
    

    Working example: http://jsfiddle.net/CPFns/

    Now the code will run each time the checkbox changes.

    If you only wanted it to run when the page loads, then get rid of the change handler, but again be sure to pass true and false instead of "true" and "false".


    Here’s a more concise way to write it.

    $(function() {
        $('#agree_to_rules').change(function() {
            $('#tweet_your_entry').attr('disabled', this.checked)
                .css({ color: this.checked ? '#666666' : '#f5f5f5' });
        });
    });
    

    Working example: http://jsfiddle.net/CPFns/1/


    EDIT:

    Based on the fiddle provided, I think this is a little cleaner solution:

    $(function() {
        $("button, input:submit, a", ".actions").button();
        $('#agree_to_rules').change(function() {
            $('#tweet_your_entry')
                .attr('disabled', !this.checked)
                .toggleClass('ui-button-disabled ui-state-disabled', !this.checked);
    
        }).change();
    });
    

    Example: http://jsfiddle.net/psBQ7/2/

    It seems that there’s some issue with jQueryUI, where its hover functionality will never be enabled if the button was disabled when the page loads.

    I removed the disabled="disabled" from the HTML, and simply triggered the .change() on page load, which will see that the box isn’t checked, therefore disabling the button after jQueryUI has been applied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Zend_Form, how would I create form elements like this: <input type=text name=element[1] value=
I have some elements <input type="text" id="test_value" name="test_value" value="xyz" /> <input id="test_default" name="test_default" type="checkbox"
I'm trying to insert a link after form elements to clear them. This demo
I'm trying to put together some jQuery code that will add form elements (input
I'm trying to apply a class to the form generated by button_to in Rails
I have an HTML form generated by JSF which maps an input element to
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
This week I have figured out how to modify form elements in the location
I have a server-generated html like: <ul> <li><!-- few nested elements that form a
I have a form that is generated based on several DataTemplate elements. One of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.