Modelesq
Modelesq

Reputation: 5382

Check if a specific radio button has been selected Jquery

What I'm trying to accomplish is:

When a user clicks the first Open until Filled option, leave the date input disabled. But when the user clicks on the second Open Until radio button. Have the date input enabled.

As of right now I can't seem to get it working. It seems to skip my check altogether. Am I missing something?

$(function() {
    if ($('input#id_open:checked').length > 0) {
        alert("please fill in the date");
        $('input[name=end_date]').attr('disabled', false);
    }
});

I made a fiddle here to show what I mean.

Thanks for your help in advance!

Upvotes: 0

Views: 288

Answers (2)

What have you tried
What have you tried

Reputation: 11138

Working example: http://jsfiddle.net/8KggV/5/

$(function() {
    $('input').click(function(){
    if ($('#id_open:checked').length > 0) {
        $('input[name=end_date]').attr('disabled', false);
    }
        else{
            $('input[name=end_date]').attr('disabled', true);        
        }
    })
});

Upvotes: 2

DragonZero
DragonZero

Reputation: 845

when using jQuery, to detect if a control is checked I use the is method. As well you don't need to check the length, as the is method return a boolean.

$(function() {
    if ($('input#id_open').is(':checked')) {
        alert("please fill in the date");
        $('input[name=end_date]').attr('disabled', false);
    }
});

Upvotes: 1

Related Questions