Problems passing this lesson ( UNIT 3 - LESSON 9)


#1

I cant seem to be able to pass this lesson.

Here is my code:

var main = function() {
    $(".article").click(function() {
        $(document).keypress(function(event) {
            if(event.which === 111) {
                $('.current').children('.description').toggle();
            } else if(event.which === 110) {
                var currentArticle = $('.current');
                var nextArticle = currentArticle.next();
                
                currentArticle.removeClass('current');
                nextArticle.addClass('current');
            }
        });
        
        $('.article').removeClass('current');
        $('.description').hide();
        $(this).addClass('current');
        $(this).children('.description').show();
    });
}
$(document).ready(main);

#2

Let's take a look at your event handlers. You have 2, one for if the user clicks and one for if the user presses a key; currently though the keypress event is nested inside of the click event. Your code is not working as desired because the keypress event can't trigger except inside of a click event. I took a good long look at your code and I believe it's written correctly, just needs to be rearranged, like this:

var main = function() {
  $('.article').click(function() {
    $('.article').removeClass('current');
    $('.description').hide();
    $(this).addClass('current');
    $(this).children('.description').show();
});

  $(document).keypress(function(event) {
    if(event.which === 111) {
       $('.current').children('.description').toggle();
    } else if(event.which=== 110) {
        var currentArticle = $('.current');
        var nextArticle = currentArticle.next();
        currentArticle.removeClass('current');
        nextArticle.addClass('current');
    };
    
});

};

$(document).ready(main);

Do you see how I have separated the 2 different types of events? They are both inside the main function of course, but the click event does not include the keypress event. The "});" series of characters on line 7 denotes the end of the click event, I then start the keypress event entirely separate.

When writing jQuery code for these exercises don't make to much out of the order in which you write the events. Everything takes place inside the main function, but each event is triggered by a different input so it is more important to keep the events separate.


#3

Yep. It works now. Thank you.


#5