dimanche 5 novembre 2017

Why my JavaScript about checkbox checked event is not workting

I have 3 checkboxes, I want it like user can not choose more than 2 boxes. But my code has something wrong, I can't get any checkbox checked event work.

HTML

<input type="checkbox" id="s1" name="service"  onchange="return count()">Service 1
<input type="checkbox" id="s2" name="service"  onchange="return count()">Service 2
<input type="checkbox" id="s3" name="service"  onchange="return count()">Service 3

JS

function count() 
    {
        var NewCount = 0;
        var s1 = document.getElementsById("s1");
    var s2 = document.getElementsById("s2");
    var s3 = document.getElementsById("s3");
        if (s1.checked)
        {               
            NewCount = NewCount + 1
            alert("for testing");
        }

        if (s2.checked)
        {
            NewCount = NewCount + 1
      alert("for testing");
        }

        if (s3.checked)
        {
            NewCount = NewCount + 1
      alert("for testing");
        }

        if (NewCount == 3)
        {
            alert('Pick Just Two Please');
            return false;
        }
    } 

This is the code which i uploaded to JSFiddle




Aucun commentaire:

Enregistrer un commentaire