RSS

Category Archives: jQuery

Values regarded as conditional false in Javascript

These values are treated as false in conditional statements, all other values including the string ‘false’ are treated as true:

Value Type
0 Number
NaN Number
”(empty string) String
false Boolean
null Object
undefined Undefined

For example, the code below will alert “NaN is falsy”: Read the rest of this entry »

Advertisements
 
Leave a comment

Posted by on September 13, 2012 in Javascript, jQuery

 

this, $(this), $this and that!

These terms may confuse beginners in learning jQuery.

this in a javascript function closure refers to the object invoking that function, for example:

var target = {
    getObject: function () {
        return this;
    }
};

alert(target.getObject());    // target object

Read the rest of this entry »

 
Leave a comment

Posted by on August 30, 2012 in Javascript, jQuery

 

Handling keypress event in jQuery

To determine which key is pressed by the user, simply check jQuery event.which property for the keycode. The example below binds textarea elements with keypress event and cancel the default Enter key function:

$("textarea").keypress(function(e) {
    // If the key pressed is Enter
    if ( e.which === 13 ) {
        // Disable line break
        e.preventDefault();

        // Do things

    }
});

Here are 2 useful links to look up javascript keycode:
Javascript Char Codes (Key Codes) – Cambia Research
JavaScript Event KeyCode Test Page

 
Leave a comment

Posted by on August 28, 2012 in Javascript, jQuery