jeudi 2 mars 2017

onmousedown event object behaving strangely

<!doctype html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>testing</title>
        </head>
        <body>
            <input type="checkbox" id="test">
            <script>
                var t = document.getElementById("test");
                t.onmousedown = function(e) {
                    if (e.button === 0)
                        t.checked = true;
                    else if (e.button === 2)
                        t.checked = false;

                    alert(e.button);
                }
            </script>
        </body>
    </html>

If I leave the line alert(e.button); where it is, checkbox clicking behaves as expected: all left-clicks check the checkbox, and all right-clicks uncheck the checkbox.

If I remove the code alert(e.button);, then all of a sudden, a left-click will check and then immediately uncheck the checkbox, and a right-click will do nothing but open the context menu.

Why does this happen? and What can I do to make it behave as I described it in the first paragraph but without alert(e.button);?




Aucun commentaire:

Enregistrer un commentaire