0

I have this code:

if(document.getElementById('UserType_2').checked) {
     if(a==null || a=="", b==null || b=="", 
        c==null || c=="", d==null || d=="",
        k==null || k=="", e==null || e=="", 
        g==null || g=="", h==null || h=="", 
        j==null || j=="", k==null || k=="") {
        document.forms["619new"]["next1"].disabled = true;
        document.forms["619new"]["next1"].style.background='#FDFDFD';
        document.forms["619new"]["next1"].style.color = 'lightgray';
        document.forms["619new"]["next1"].style.border = 'none';
        document.forms["619new"]["next1"].style.cursor = 'not-allowed';
    }
    else {
        document.forms["619new"]["next1"].disabled = false;
        document.forms["619new"]["next1"].style.background='#195dad';
        document.forms["619new"]["next1"].style.color = 'lightgray';
        document.forms["619new"]["next1"].style.border = 'none';
        document.forms["619new"]["next1"].style.cursor = 'pointer';
    }
}

It is meant to disable a button unless the fields are entered. Once entered button becomes clickable.

This works. HOWEVER... If i decide to fill the last field in first it overrides all the other fields and unlocks the button when the others are empty.

I have tried to find a fix but have become stuck. Can anyone help?

Loofer
  • 6,841
  • 9
  • 61
  • 102
Philb24
  • 67
  • 1
  • 9

2 Answers2

2

The reason you are seeing this behavior is because you are using comas inside the if statement. To my surprise javascript accepts them just fine but the result of the operation is the value of the last operand. check this answer here Why does javascript accept commas in if statements?

Update:

Change the coma to || then try changing the way you add and remove the disabled attribute from the button. And last try to use ! to negate your statements because what you are checking for does not take into account the case for undefined.

if(document.getElementById('UserType_2').checked) {
    if(!a || !b || !c ||
       !d || !e || !f || !g || 
       !h || !i || !j || !k ) {
        document.forms["619new"].next1.disabled = "disabled";
        document.forms["619new"].next1.style.background='#FDFDFD';
        document.forms["619new"].next1.style.color = 'lightgray';
        document.forms["619new"].next1.style.border = 'none';
        document.forms["619new"].next1.style.cursor = 'not-allowed';
    }
    else {
        delete document.forms["619new"].next1.disabled;
        document.forms["619new"].next1.style.background='#195dad';
        document.forms["619new"].next1.style.color = 'lightgray';
        document.forms["619new"].next1.style.border = 'none';
        document.forms["619new"].next1.style.cursor = 'pointer';
    }
}
Community
  • 1
  • 1
cleftheris
  • 4,626
  • 38
  • 55
-1

Like @Adjit said just use || instead of the commas. Thanks thought i tried it before lucky i gave it another go!

Philb24
  • 67
  • 1
  • 9
  • Why/how does this aid in the question/answer system? – Bonatti Oct 23 '15 at 10:34
  • Then, please, either edit your answer to state what helped the issue (so taht new users may read and fix their problems), or check as "correct" the answer that lead you to fix the issue.... Do this by clicking the green "checkmark" below the arrows, on the left of the answer – Bonatti Oct 23 '15 at 16:14