Kategorie
Bez kategorii

Task 8

HTML code for press event

<html>
<head>
    <title>Dawid Andrejczuk</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(function() {
            var count = 0;

            $(document).on("keypress", function() {
                count++;

                if (count == 10) {
                    alert("You won!");
                }
            });
        });
    </script>
</head>
<body>
    <h1>Keypress Counter for task 8</h1>
    <p>Press any key on your keyboard to count the number of keypress events. When you reach 10 keypresses, you win a prize!</p>
</body>
</html>

This is a simple HTML page that contains a JavaScript code to count the number of keypress events and display an alert message when the count reaches 10.

When the page is loaded, jQuery library is loaded from a CDN using the <script> tag. Then, the $(function() {…}) code block is executed when the page is ready. This block initializes a variable count to zero.

The $(document).on(„keypress”, function() {…}) function binds an event listener to the document object that listens for a keypress event. When a key is pressed, the anonymous function is executed, incrementing the count variable by 1. If the count variable is equal to 10, an alert message is displayed using the alert() function.

The HTML part of the code simply contains a heading and a paragraph explaining what the page does. When the user presses a key, the counter increments and the user wins when they reach 10 keypress events.

To see counter press F12 and chose console.