multiple if statements check optimize My code has mutiple checks for if staments which I think could be optimized into few lines if I take id into varibable. To give some background I want to disable a button based on length of characters in input box. But the thing is out of two buttons only one will be rendered at a time. 
Below is my working version of code which I am hoping to optimize.

  var sortcodeError = document.getElementById("sortcodeError")
         var accountnameError = document.getElementById("accountnameError")
          if (sortcodeError != null) {
                  document.getElementById("sortcodeError").disabled = false;
               }
               if (accountnameError != null) {
                  document.getElementById("accountnameError").disabled = false;
               }
         if ($scope.data.branchTransitNumber &amp;&amp; $scope.data.depositAccountNumber) {
            if ($scope.data.branchTransitNumber.length &lt; 6) {
               if (sortcodeError != null) {
                  document.getElementById("sortcodeError").disabled = true;
               }
               if (accountnameError != null) {
                  document.getElementById("accountnameError").disabled = true;
               }

            }
            if ($scope.data.depositAccountNumber.length &lt; 8) {
               if (sortcodeError != null) {
                  document.getElementById("sortcodeError").disabled = true;
               }
               if (accountnameError != null) {
                  document.getElementById("accountnameError").disabled = true;
                  // angular.element($('#btnSubmi')).addClass("gray");
               }

            }
         }


My initial thought is if I could take id as varibale but then I don't think it is the best way
Since exactly one element will exist at a time, you can retrieve it by alternating with || between both getElementById calls. Then, there's no need for any existence test later.

Since you set disabled to true in both branchTransitNumber.length &lt; 6 and depositAccountNumber.length &lt; 8, you can combine those blocks together:

const button = document.getElementById('sortcodeError') || document.getElementById('accountnameError');
button.disabled = false;
if ($scope.data.branchTransitNumber &amp;&amp; $scope.data.depositAccountNumber) {
    if ($scope.data.branchTransitNumber.length &lt; 6 || $scope.data.depositAccountNumber.length &lt; 8) {
        button.disabled = true;
    }
}


(The above can be made even more concise via destructuring or optional chaining to avoid the double ifs, but I think it'd look too confusing.)

Or, if you need the addClass call only in the case of $scope.data.depositAccountNumber.length &lt; 8 and accountnameError existing:

const button = document.getElementById('sortcodeError') || document.getElementById('accountnameError');
button.disabled = false;
if ($scope.data.branchTransitNumber &amp;&amp; $scope.data.depositAccountNumber) {
    const depositNumberProblem = $scope.data.depositAccountNumber.length &lt; 8;
    if ($scope.data.branchTransitNumber.length &lt; 6 || depositNumberProblem) {
        button.disabled = true;
        if (depositNumberProblem &amp;&amp; button.id === 'accountnameError') {
            angular.element($('#btnSubmi')).addClass("gray");
        }
    }
}


Depending on the logic of your script, the above may be simplifiable. For example, you may wish to do .addClass("gray") regardless when you disable the button.

Other notes:


It's 2020, best to at least use ES2015 syntax in new code by declaring variables with const, not var (which has too many gotchas)
Whenever you do have to compare two different values, use strict equality with === or !== - avoid loose equality == and !=, since loose equality has too many strange rules that a developer should not have to memorize.
If the alternation trick above weren't possible, you'd still be able to reference the variable names you saved the elements into to keep things concise. Eg, with const sortcodeError = document.getElementById("sortcodeError"), you should never have to call document.getElementById("sortcodeError") later - instead, just reference the sortcodeError variable.
Use consistent indentation. If one statement is in the same block as another, it should begin at the same indentation level. This significantly improves a code's readability.
Either use semicolons wherever appropriate, or don't. (If you're not an expert, I'd recommend using them, otherwise you'll occasionally run into strange bugs.) Consider using a linter to automatically prompt you to fix potential mistakes (and to make the code nicer-looking).
The overall logic looks a bit weird. Do you really need to check that both branchTransitNumber and depositAccountNumber exist, but disable a button if either of them fail a length test? In a validation situation, I'd expect to disable a button if either fail, rather than if both exist and either fail. Double-check the logical paths your code requires and results in, and consider what sort of effect you really want.