"N Shortcut 2" - this code is breaking my head!


#1

I'm stuck with this code ,,and it's breaking my head,just enough

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) {
         else if(event.which===110) {
             var currentArticle = $('.current');
             var nextArticle = currentArticle.next();

             currentArticle.removeClass('current');
             nextArticle.addClass('current');
         }
         $('.current').children('.description').toggle();
     }
});

}
;
};

$(document).ready (main);

where this I get wrong?


Getting the error message "SyntaxError: Parse Error" on The N Shortcut 2
#2

Hi Daniel,

You need to separate your else if statement so it comes after your if statement.

The syntax for an if/else if statement is:

if (condition) {
  // ...
}
else if (otherCondition) {
  // ...
}

...not

if (condition) {
  else if (otherCondition) {
    // ...
  }
  
  // ...
}

:wink:


#3

@cloudninja71720,
You have pasted-in

        else if(event.which===110) {
             var currentArticle = $('.current');
             var nextArticle = currentArticle.next();

             currentArticle.removeClass('current');
             nextArticle.addClass('current');
         }

into the middle of your IF-statement

     if(event.which===111) {
         $('.current').children('.description').toggle();
     }

So the document =keypress= Eventhandler shoud look
like

$(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');
    }

});

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

and
you have 1 closing-curly-bracket-} too many

the structure of our main-function looks like:

    var main = function() {
         //BEGIN of main function-body 
        //
        //The CLICK event-handler is

        $('.article').click(function() {

         });
    
         //now comes the KEYPRESS event-handler

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

         });
       //END of main function-body
       };
    
      $(document).ready(main);

#4

Thanks' M. Leonhard , it took me a while to respond and my apologies' for that , you have help me a lot ,just some simple mistake I did , but after a while of deleting and adding stuff, that I lost track, I'm going to repeat the lesson until the knowledge is well acquired , thanks' again and hope to chat some more in the future.

Daniel.


#5

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.