Not sure what this parameter does


#1

Here is the whole JavaScript 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();
        }
        
        });


    };

$(document).ready(main);

My problem is I can't figure out what the parameter event does in

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

I tried removing the event parameter and it still works. I hope someone can help me out. Thanks! :slightly_smiling:


#2

@melbaylon,
In the jQuery documentation
google search
== the Book ==
keypress() site:jQuery.com
https://api.jquery.com/category/events/event-object/

you see that the keypress() Method
will have a returned object event

This event object
has several so-called properties.
A property consists of a property-key and its associated Value
[quote]
Event Properties

jQuery normalizes the following properties for cross-browser consistency:

  • target ===>associated Value ==> event.target
  • relatedTarget ===>associated Value ==> event.relatedTarget
  • pageX ===>associated Value ==> event.pageX
  • pageY ===>associated Value ==> event.pageY
  • which ===>associated Value ==> event.which
  • metaKey ===>associated Value ==> event.metaKey

The following properties are also copied to the event object, though some of their values may be undefined depending on the event:

altKey, bubbles, button, buttons, cancelable, char, charCode, clientX, clientY, ctrlKey, currentTarget, data, detail, eventPhase, key, keyCode, metaKey, offsetX, offsetY, originalTarget, pageX, pageY, relatedTarget, screenX, screenY, shiftKey, target, toElement, view, which

If you would update your document =keypress= Eventhandler to

$(document).keypress(function(event) {
    $('<li>').text("event.target=>"+event.target).appendTo('body');
    $('<li>').text("event.relatedTarget==>"+event.relatedTarget).appendTo('body');
    $('<li>').text("event.pageX==>"+event.pageX).appendTo('body');
    $('<li>').text("event.pageY==>"+event.pageY).appendTo('body');
    $('<li>').text("event.which==>"+event.which).appendTo('body');
    $('<li>').text("event.metaKey==>"+event.metaKey).appendTo('body');
 if(event.which === 111) {
 $('.current').children('.description').toggle();
 }
 });

you will have to scroll to the bottom of the Page-Display
and you will see some data.

The associated which.Value
can be looked up in
http://msdn.microsoft.com/en-us/library/60ecse8t(v=vs.80).aspx
look in the Decimal column..( and take care of looking for the lowercase characters )

google search
== the Book ==
jquery [your question] site:developer.mozilla.org
CSS [your question] site:developer.mozilla.org
javascript [your question] site:developer.mozilla.org
[your question] site:jquery.com
[your question] site:getbootstrap.com

== discussions / opinions ==
jquery [your question] site:stackoverflow.com
CSS [your question] site:stackoverflow.com
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
[your question] site:crockford.com
- - http://javascript.crockford.com/code.html <<==Javascript code-convention's !!
- - http://javascript.crockford.com/survey.html

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