0

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);
    }
2
  • $("input:checkbox[value='"+disableId +"']).attr("disabled", true); Commented Jan 31, 2014 at 14:14
  • In case you go straight to Anton's upvoted answer, I'll just point out here what I've said in my answer: the if statement you're using is unnecessary as you can simply pass in e.currentTarget.checked in place of true or false. You should also use prop() instead of attr(). Commented Jan 31, 2014 at 14:25

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

Open the string and add it

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.