Greg Gum
Greg Gum

Reputation: 37909

How to use variable in JQuery select

How do I modify the jquery to use the var instead of the '12'?

    var disableId = 12;

    if (e.currentTarget.checked) {

        $("input:checkbox[value=12]").attr("disabled", true);
    }
    else {
        $("input:checkbox[value=12]").attr("disabled", false);
    }

Upvotes: 0

Views: 51

Answers (2)

Anton
Anton

Reputation: 32581

Open the string and add it

$("input:checkbox[value='"+disableId +"']").prop("disabled", true);

also you should use prop() instead of attr for boleean values

Upvotes: 1

James Donnelly
James Donnelly

Reputation: 128791

You should use jQuery's prop() method instead of attr() for boolean values; also there's no need for the IF statements at all as you can just set the disabled state to equal e.currentTarget.checked (which is either true or false):

var disableId = 12,
    state = e.currentTarget.checked;

$("input:checkbox[value='" + disableId + "']").prop("disabled", state);

Upvotes: 1

Related Questions