Mouse(Event) button(Property) tutorial in JavaScript

 <!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Mouse(Event) Button(Property) JavaScript</title>
    <link rel="stylesheet" href="css/Mouse(Event)_Button(Property)_JavaScript.css">
</head>

<body>
    <div id="box"></div>
    <script>
        var target = document.querySelector("#box");
        target.addEventListener("mousedown", function (e) {
            console.clear();
            var text = "You Pressed mouse button : " + e.button;
            console.log(text);

            var textTwo = e.button;
            var color;

            switch (textTwo) {
                case 0:
                    color = "green";
                    break;
                case 1:
                    color = "blue";
                    break;
                case 2:
                    color = "red";
                    break;
                default:
                    color = "pink";
                    break;
            }
            target.style.backgroundColor = color;
            console.log(color);
        });
    </script>
</body>

</html>




Below File is css/Mouse(Event)_Button(Property)_JavaScript.css File
#box {
    width: 400px;
    height: 300px;
    border: 1px solid #000000;
    background: #ffc0cb;
    margin: 20px;
}



Comments

Popular posts from this blog

Generators tutorial in Advance JavaScript

Document Object Module DOM querySelector and querySelectorAll tutorial in JavaScript

Find Even and Odd Numbers with Loops tutorial in JavaScript