9. the n shortcut 2


#1

I keep receiving this error;
Oops, try again. It looks like the n key isn't moving to the next article. Remember to remove the 'current' class from currentArticle and place it on nextArticle.

this is my code;

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);

can anyone see what's wrong with it?


#2

@hanna98789,
You have to take care where you are using closing curly-brackets-}....

var main = function(){
    //Begin of =main= FUNCTION-BODY

    $('.article').click(function(){
         //function code-block
         }
      ); //closing =click= Eventhandler
}; //closing the =main= FUNCTION-BODY

and therewith the rest of your code would not be part of this =main= function.

===================================================

The desired code-structure would have to look like

var main = function(){
    $('.article').click(function(){
        // +article+ =click= Eventhandler with it's function-code-block
    });

    $(document).keypress(function(event){

        if ( condition ) {
            // IF code-block
        } 
        else if ( condition ) {
            // ELSE-IF code-block
        }

    });
}
$(document).ready(main);