Wednesday, March 28, 2012

Solution to Chapter 3 Exercises of Learning jQuery 3rd Edition


My solutions to Learning jQuery 3rd Edition Chapter 4 Exercises:
1. When Charles Dickens is clicked, apply the selected style to it.

$(document).ready(function ($) {
    $('.author').click(function () {
        $(this).addClass('selected');
    });
});



2. When a chapter title (<h3 class="chapter-title">) is double-clicked, toggle the visibility of the chapter text. 
(look also at the comments section for a shorter solution to this problem)
$(document).ready(function ($) {
    var toggleVisibility = function () {
        $(this).toggle(
            function () {
                $(this).nextAll().addClass('hidden');
            },
            function () {
                $(this).nextAll().removeClass('hidden');
            }
        );
    };

    $('.chapter-title').dblclick(toggleVisibility);
});



3. When the user presses the right arrow key, cycle to the next body class. The key code for the right arrow key is 39.

$(document).ready(function ($) {
   
var bodyClass = ["default", "narrow", "large"];
    var numberOfBodyClasses = 3;
    var currentBodyClassNumber = 0;


    // setBodyClass() & toggleSwitcher() ARE FROM THE BOOK'S CODE
    // Allow the style switcher to expand and collapse.
    var toggleSwitcher = function (event) {
        if (!$(event.target).is('button')) {
            $('#switcher button').toggleClass('hidden');
        }
    };
    // The setBodyClass() function changes the page style.
    // The style switcher state is also updated.
    var setBodyClass = function (className) {
        $('body').removeClass().addClass(className);

        $('#switcher button').removeClass('selected');
        $('#switcher-' + className).addClass('selected');

        $('#switcher').unbind('click', toggleSwitcher);

        if (className == 'default') {
            $('#switcher').bind('click', toggleSwitcher);
        }
    };

    $(document).keyup(function (event) {
        currentBodyClassNumber = (currentBodyClassNumber + 1) % numberOfBodyClasses;
        if (event.keyCode == 39) // 39 is key code for right arrow key
        {
            setBodyClass(bodyClass[currentBodyClassNumber]);
        }
    });

});


4. Challenge: Use the console.log() function to log the coordinates of the mouse as it moves across any  paragraph. (Note: console.log() displays its results via the Firebug extension for Firefox, Safari's Web Inspector, or the Developer Tools in Chrome).

$(document).ready(function ($) {
    $('p').mousemove(function (event) {
        console.log("Mouse location: (" + event.pageX + ", " + event.pageY + ")");
    });

 });



5. Challenge: Use .mousedown() and .mouseup() to track mouse events anywhere on the page. If the mouse button is released above where it was pressed, then add the hidden class to all paragraphs. If it is released below where it was pressed, then remove the hidden class from all paragraphs.

$(document).ready(function ($) {
    var mouseDownYLocation = 0;

    $(document).mousedown(function (event) {
        mouseDownYLocation = event.pageY;
    });
   
    $(document).mouseup(function (event) {
        if (event.pageY < mouseDownYLocation) //if mouse button is released above where it was pressed
        {
            $('p').addClass('hidden');
        }
       else if(event.pageY > mouseDownYLocation) //if mouse button is released below where it was pressed
        {
            $('p').removeClass('hidden');
        }
    });
});


This is how the page looks after I clicked Charles Dickens (Exer 1), double-clicked Preface (Exer 2), pressed right arrow key once (Exer 3), and moved the mouse across paragraphs (Exer 4).






  
Free Lessons for Chruch Pianists:
Free lessons, tips and downloads for church pianists

DVD Courses (Reharmonization, Play by Ear!, Arranging, Accompanying, Theory for Church Pianists, etc.):
Over 30 hours of DVD instruction for church pianists
 


Visit Vitabase for Quality Health Supplements