1

I'm trying to write an javascript function to disable certain fields on a visualforce page if a checkmark field ("locked") is checked. The javascript is failing and I've isolated the problem to my if statement. Can someone tell what I"m doing wrong here? (If I comment out the if statement the rest of the script works fine, and it is getting the value of PA.locked__c fine as well)

setTimeout(function(){ 
                   If("{!PA.Locked__c}" == true) {
                       //alert ({!PA.Locked__c});
                       //$('select').css({"border": "transparent","background": "transparent","box-shadow": "none"}).attr('disabled','disabled');
                       //$('textarea').css({"border": "transparent","background": "transparent","box-shadow": "none"}).attr('disabled','disabled');
                       //$('input[type="text"]').css({"border": "transparent","background": "transparent","box-shadow": "none"}).attr('disabled','disabled');
                  }
                }, 1);
3

JavaScript is cAsE-sEnSiTiVe, so If is basically a variable name, while if is a keyword that denotes a branching instruction. Also, you should not compare a Boolean to a String, and you should use strict equality if you're going to do so. All that said, I typically advise that you skip the comparison, because a Boolean value is already a Boolean value. Thus your code should look like this:

if({!PA.Locked__c}) {
0

EDIT:Updated because I got it wrong the first time.

Yes to what @sfdcfox wrote, but also - as written, if PA.Locked__c is true, your JS looks like:

If("true" == true) {...}

In JavaScript, this will evaluate to FALSE.

3
  • Actually, "false" == true is false (just tried it to confirm my sanity). However, if("false") would be true, which is why I recommended removing the quotes entirely. – sfdcfox Nov 30 '17 at 15:25
  • You're right, my mistake. In that case, the reverse case is the problem. If PA.Locked__c is false, you get If("true" == true) which evaluates to FALSE. – Charles T Nov 30 '17 at 18:40
  • Yeah, that's a good reason to avoid using that as well. I always recommend not comparing to true/false anyways, because a Boolean is already a Boolean. – sfdcfox Dec 1 '17 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.