Intelwalk
Intelwalk

Reputation: 671

JQuery "greying" out textfield in form

I have the following function, but it does not blur out when the radio button is selected to no. Any ideas why?

Form Element:

<td>
    Email: Yes? <input type="radio" name="emailquest" value="true" checked>
    No? <input type="radio" name="emailquest" value="false">
</td>
<td>
    <input type="text" name="email">
</td>

Script:

<script>
    $(document).ready(function(){
        $("#emailquest").blur(function(){
            if ($(this).val() != true)
                $("#email").attr("disabled","disabled");
            else
                $("#email").removeAttr("disabled");
        });
    });                     
</script>

Upvotes: 1

Views: 79

Answers (1)

tymeJV
tymeJV

Reputation: 104775

As Pointy said, # is used for ID's, not names

$("input[name='emailquest']").change(function(){
    if (this.value != "true") { // <----I would probably change this to look for this.checked
        $("input[name='email']").prop("disabled", true);
    } else {
        $("input[name='email']").prop("disabled", false);
    }
});

Live demo: http://jsfiddle.net/E3zDB/

Shortened version using a ternary operator:

$("input[name='emailquest']").change(function(){
    var input = $("input[name='email']");
    this.value != "true" ? input.prop("disabled", true) : input.prop("disabled", false);
});

Upvotes: 6

Related Questions