0

i have to disable the checkbox dynamically based on the lead and account first name. Scenario is if lead and account first name are equal the checkbox should be disabled. Here is my code

var firstName = '{!leaRec.FirstName}'.localeCompare('{!acc.FirstName}');
if(firstName ==0){
document.getElementById("checkid").disabled = true;
}
else {
alert('fail');
document.getElementById("checkid").disabled = false;
}

<input type = "checkbox" id = "checkid"/>

i am getting "cannot set property disabled of null". please help me out .

  • 2
    Checkbox ids are different in if block and else block. Is that correct? – highfive Jul 10 '16 at 4:18
  • sorry while pasting here i have done it wrong. but in page actually it is correct.Now i have edited the question – user34256 Jul 10 '16 at 4:35
4

If I had to guess, I'd say that you're calling your script directly (i.e. is is not run after the page loads), and your input element is after the script's source within the page. This will cause your code to fail, because the element does not exist in the DOM at the time you're trying to access it. Instead, make sure you wait for the page to be ready:

window.addEventListener('DOMContentLoaded', function() {
    var firstName = '{!JSENCODE(leaRec.FirstName)}'.localeCompare('{!JSENCODE(acc.FirstName)}');
if(!firstName){
document.getElementById("checkid").disabled = true;
}
else {
alert('fail');
document.getElementById("checkid").disabled = false;
}})
  • yes @sfdcfox. you are right. Now i have done it through window.onload .. thanks for your input :) – user34256 Jul 10 '16 at 4:54

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.