9. The n shortcut 2


#1

var main = function() {
$('.article').click(function() {
$('.article').removeClass('curent');
$('.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);

What's wrong in here?


#2

@chipsurfer11311,

var main=function() {
     $('.article').click(function() { 
     }); 

     $(document).keypress(function(event) {
     });
   };
  $(document).ready(main);

In our main function we are defining 2 Event-handler
1 a click Event-handler attached to all articles
2 a keypress Event-handler attached to the whole document

= = = = = = a click Event-handler attached to all articles = = = = =

In this Event-handler we want to do 4 things:
1 Select-all-Elements which have the class-atribute "article" set
- and remove the Class-attribute "current"
- $('.articles').removeClass('current');

2 Select-all-Elements which have the class-attribute "description" set
- and perform the hide Method on these Elements.
- $('description').hide();

3 Select-the-Element which was just clicked-on using $(this)
- and add the Class-attribute _'current'
- $(this).addClass('current');

4 Select-the-Element which was just clicked-on using $(this)
- we now have access to the whole of the DOM-tree of this Element
- and out of this selection of Elements represented in the DOM-tree,
- and are the so-called Children of $(this)-Element,
- choose those Element(s) which have the Class-attribute 'description' set
- and on this sub-selection perform the show Method.
- $(this).children('.description').show()

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

The IF ELSE-IF-statement must be read as:
The IF-verb,
its condition in parenthesis ( )
and a code-block encapsulated in opening & closing curly-brackets-{ }

if ( condition1 ) {
// this code which will be executed IF CONDITION1 is TRUE
}

Now if condition1 rendered to false the if { code-block} is NOT executed
and if the JavaScript-parser finds an ELSE-IF-statement
it will start to evaluate that statement,
with it's condition2
and if this condition2 renders to true,
the { code-block } will be executed.

if ( condition1 ) {
     // this code which will be executed IF CONDITION1 is TRUE
}
else if ( condition2 ) {
        // condition1 ran into a -false-
        // this code which will be executed IF CONDITION2 is TRUE
}

Now encapsulated within the keypress Event-handler
as its function-body

$(document).keypress( function(event) {
    if ( condition1 ) {
        // this code which will be executed IF CONDITION1 is TRUE
    }
    else if ( condition2 ) {
       // condition1 ran into a -false-
       // this code which will be executed IF CONDITION2 is TRUE
    }
  });

++++++++++++++++++++++++++++++++++++++++

As a function of the keypress Event-handler we are going to evaluate
the object which we get as a return value in the variable event.
_( An object has one or more properties
Each property consists of a property-key and it's associated VALUE )_

If we write event.which
we are reading the associated VALUE of the which property-key.
We are using the Decimal column of the table
http://msdn.microsoft.com/en-us/library/60ecse8t(v=vs.80).aspx
( arrow left-37 up-38 right-39 down-40 )
and we see that the letter o is represented with the Decimal value 111

if ( event.which === 111 ) {
// if you have made the preview-screen on-focus by clicking in the 'green'
// and you have pressed the o-keyboard-key
// the "description" will be shown
// of the article which is carrying the Class-attribute 'current'.
} herewith we close the if code-block

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

Now if didn't press the o key but for instance the n-keyboard-key
(looking in the Decimal column we see the VALUE 110)
Now we are entering the ELSE-IF
else if ( event.which === 110) {
// we are going to capture the jQuery object which we get
// as return VALUE of using the jQuery-Selector $('.current')
// var currentArticle = $('.current');

// as all articles are Siblings to each other,
// we can use the next() Method
// to orientate ourselves on the next 'article'
// and capture the returned jQuery Object
//we assign this returned jQuery Object to its own variable
// var nextArticle = currentArticle.next();

// now want to remove the Class-attribute 'current' from the currentArticle
// currentArticle.removeClass('current');
// we want the next-'article' to carry this Class-attribute 'current' by addClass
//**nextArticle.addClass('current');**
// you will see the current'article' will lose it's highlight
// and the next'article' will be highlighted.
} we close the ELSE-IF statement !!

  $(document).keypress(function(event) {
     // if the "o" keyboard-key is pressed
     if ( event.which === 111 ) {
       $('.current').children('.description').toggle()
     }
     // if the "n" keyboard-key is pressed
     else if ( event.which === 110 ) {
       var currentArticle = $('.current');
       var nextArticle = currentArticle.next();
       // swap the Class-attribute 'current'
       currentArticle.removeClass('current');  //it loses highlight
       nextArticle.addClass('current');  // it is highlighted
     }
     // if the "a" keyboard-key is pressed.
     else if ( event.which = 97 ) {
        $('.item').toggle();
        $('.description').toggle();
     }
    });

#3

@chipsurfer11311,

If you use $( )
you are using the so called jQuery selector

The selector will select ALL the HTML-Elements of the active HTML-page,
which have an attribute equal to the one you use as an argument.

Thus if you use
$(".article") you are selecting all the HTML-Element's
which have the class="article" set
( you will find 7 Article's)

The jQuery Selector will create an object
which will contain all those selected Elements. AND its children..

Talking in terms of DOM (=Document Object Model )
you select a parent and all its children which are in its tree

Here is the how the Document Object Model view looks like for
this HTML-page..

                   html
                    |
      + - - - - - - + - - - - - -+
      |                          |
      3                          7
     head                       body
      |                          |
  + - + - +          + - - - - - + - - - - - - +
  |       |          |           |             |
  4       5          8          165           166
 link    link       div        script        script
                 .articles
                 .container
                     |
 + - - - + - - - + - + - + - - - + - - - + - - - +
 |       |       |       |       |       |       |
 10      32      54      76      98     120     142
 div     div     div     div     div    div     div  < .article
                      .current                     sibling's  
                          |                          .next()
           +- - - - - - - + - - - - - - - - +
           |                                |
           77                               88
          .item                        .description
           div                              div
            |                                |
  + - - - - + - - - -+           + - - - - - + - - - - -+
  |         |        |           |           |          |
  78        81       84          89          90         94
  div       div      div         div         div        div
                                              |
                                      + - - - + - - - +
                                      |               |
                                      h1              p

The numbers in the Diagram
represent the line-number in the index.HTML file.

So to get to the articles we follow the path
html -> 7-body -> 8-div
(we landed at the PARENT of ALL the Articles)
Those Articles are the CHILDREN to this PARENT.
And the CHILDREN between themselves are SIBLINGS.
And ALL these CHILDREN are carrying the Class article.

In this Diagram i have
put the current Class on the 4th Article...
this would happen if you clicked on the 4th-Article
at wich point you have this article at your fingers via this

$('.article').removeClass('current'); attribute class=current from all those Elements.

$('.description').hide();

$(this).addClass('current'); this as the way to identify this Element, add the Class attribute "current", which via the style.CSS file will cause the change

of color via the .current CSS-object,
but from now on if we use the jQuery Selection with the argument '.current'
like $('.current') , we know that we have selected 1 Article-Element, with ALL its CHILDREN

$(this).children('.description').show(); clicked-on Article and ALL its Elements, and from this collection of Elements, choose the Element with the Class "description" set, and show

that Element and all its Children.

\==================================
$('.description').show(); Class "description" and show them...you will see all 7 Articles with their description shown.

$('.current').children('.item').toggle()
$('.current').children('.description').toggle()
step 1 , hide the Article-line using the ".item" Class as sub-selection
step 2 , show the Children Elements of the Parent with Class "description"


#4

first, on line 8 the } closed your main function, it should showed up all the way at the end of code before
$(document).ready(main);

second, you did not close your if statement, a } is expected after this code. $('.current').children('.description').toggle();