To Lecture Notes

IT 238 -- Feb 23, 2026

Review Exercises

  1. Display this image of the Andromeda Galaxy on an HTML page. When the image is clicked, use JavaScript to add a border, a margin, and padding to the image. Answer:
    <!DOCTYPE html>
    <!-- AndromedaImage Example
         Source code file: index.html -->
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" 
                content="width=device-width, initial-scale=1.0">
            <title>Andromeda Image</title>
            <style>
            img { width: 300px; }
            </style>
            <script>
            function init( ) {
                var image1 = document.querySelector("img");
                image1.style.padding = "20px";
                image1.style.border = "10px solid red";
                image1.style.margin = "30px";
            }
            document.addEventListener("DOMContentLoaded", init);
            </script>
        </head>
        <body>
            <h1>Andromeda Image</h1>
            <img src="images/andromeda.jpg">
        </body>
    </html>
    
  2. Start with this HTML body:
    <body>
        <h2>Ordered List Example</h1>
    </body>
    
    Use JavaScript to create and append this ordered list to the body:
    1. New York City
    2. Los Angeles
    3. Chicago
    Answer:
    --------------------------------------------
    <!DOCTYPE html>
    <!-- OrderedList Example
         Source code file: index.html -->
    <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" 
                content="width=device-width, initial-scale=1.0">
            <title>Ordered List Example</title>
            <link rel="stylesheet" href="styles.css">
            <script src="script.js"></script>
        </head>
        <body>
            <h2>Ordered List Example</h1>
        </body>
    </html>
    --------------------------------------------
    /* OrderedList Example
       Source code file: styles.css */
    body { font: 100% Verdana, sans-serif; }
    --------------------------------------------
    // OrderedList Example
    // Source code file: script.css
    function init( ) {
        // cities array is source of content for ordered list.
        var cities = ["New York City", "Los Angeles", "Chicago"];
    
        // Create and add ordered list to body.
        var olObj = document.createElement("ol");
        var bodyObj = document.querySelector("body");
        bodyObj.appendChild(olObj);
    
        // Add cities to ordered list as list items.
        for(var c of cities) {
            var liObj = document.createElement("li");
            liObj.innerHTML = c;
            olObj.appendChild(liObj);
        }
    }
    // DOMContentLoaded files when DOM tree has been
    // created and made available to the script.
    document.addEventListener("DOMContentLoaded", init);
    --------------------------------------------
    
  3. What is the difference between the CSS properties display and visibility for an HTML element?
    Answer: When the display property is set to "none", the item no longer takes up space and the neighboring elements close up the space around the item.  When the visibility property is set to "hidden", the invisible element still takes the same amount of space as when it was visible.

Project 2b

The jQuery JavaScript Library

Basic jQuery Syntax

Some jQuery Examples

JavaScript querySelector Method

The W3Schools jQuery Tutorial

Look at some of the examples in the jQuery Tutorial:
www.w3schools.com/jquery/default.asp.

Translate these jQuery function calls from jQuery into vanilla JavaScript. Also, write the JavaScript functions in arrow notation.

  1. Add an event handler for the window load event that displays "Hello, World!" in an alert box.
    $(function( ) {
        alert("Hello, World!");
    }
    
    or
    $(( ) => { 
        alert("Hello, World!"); 
    });
    
  2. Add an event handler to a button that displays "Hello, World!" in an alert box.
    $(function( ) {
        $("btn1").click(function( ) {
            alert("Hello, World!");
        });
    });
    
    or
    $(( ) => {
        $("bt1").click(( ) => {
            alert("Hello, World!");
        });
    });
    
  3. Try out the W3Schools double click event handler on this page:
         www.w3schools.com/jquery/jquery_events.asp
    $(function( ){
        $("p").dblclick(function( ){
            $(this).hide( );
        });
    });
    
    Translate it into vanilla JavaScript. Answer:
    function eventHandler(e) {
        var para = e.target;
        para.style.display = "none";
    }
    function init( ) {
        var para = document.querySelectorAll("p");
        for(let node of para)) {
            node.addEventListener("dblclick", 
                eventHandler);
        }
    }
    window.addEventListener("load", init);
    
    Here is the script written with anonymous functions using arrow notation:
    window.addEventListener("load", ( ) => {
        var para = document.querySelectorAll("p");
        for(let node of Array.from(para)) {
            node.addEventListener("dblclick", e => {
                var para = e.target;
                para.style.display = "none";
            });
        }
    });
    
  4. Also look the W3Schools examples for this other events:
    mouseenter  mouseleave  mousedown  
    mouseup  hover  focus  blur