The o shortcut


#1

can somebody please help me? I don't know what I'm doing wrong here thanks

var main = function() {
};

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


#2

@ptcha,

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"


#3

@ptcha,

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

#4

@ptcha,

+++++ addClass removeClass

Every HTML-Element has so called attributes.
One of the possible attributes, is the class-attribute.

If you write the HTML-document, you add the class-attribute
within the Element-Tag by using the syntax
class-keyword = then a string
containing the class-name
OR mutliple class-names which are =separated=from=eachother= by a space

Thus with

 <div class="myClass1 myClass2 myClass3" ></div>

you now have a DIV-Element carrying 3 classes myClass1, myClass2 and myClass3

As part of its CSS-category-of-Methods jQuery gives you the ability
to manipulate this class-attribute
by providing the addClass()-, removeClass()-, toggleClass()-Methods.
You will have to provide the Method at least 1 argument
which is the string VALUE of the class-name.
Thus you have to change
**$('.article').removeClass('.current')
$(this).addClass('.current')**
into
**$('.article').removeClass('current')
$(this).addClass('current')**

The confusion when to use a pre-pending dot or NOT...
In the Cascading Style Sheet syntax,
you have to prepend-a-dot to the classname
so the CSS will interpret it as a class-attribute
So in the file style.css you will find an object
identified by .current,
In this .current object
the background property-key is set to a particular VALUE

jQuery uses the same syntax to identify a class-attribute
like in the jQuery-selector
$(".current") by which this HTML-Element and all it's CHILDREN
are selected into a jQuery-object,

This parent-HTML-Element carries the class-attribute with class-name "current"

Reference::

google search
class site:jquery.com
https://api.jquery.com/category/selectors/
http://api.jquery.com/class-selector/
https://api.jquery.com/element-selector/
http://www.w3schools.com/jquery/jquery_ref_selectors.asp
http://www.w3schools.com/jquery/trysel.asp

what is CSS explained site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/What_is_CSS

Some reference-material:
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes

= http://stackoverflow.com/questions/1795438/load-and-execution-sequence-of-a-web-page