Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Monday, 24 November 2014

Assign event to Escape keypress


$(document).keyup(function(e) {
  if (e.keyCode == 27) { some_code_here }   // esc
});

Prevent link from doing default action

$( "a" ).click(function( event ) {
event.preventDefault();});
http://api.jquery.com/event.preventdefault/

Monday, 13 October 2014

Jquery multiple selectors

jQuery( "selector1, selector2, selectorN" )

Note that the commas are within the selector.

Jquery iterating over arrays, objects, and array-like objects

http://learn.jquery.com/using-jquery-core/iterating/

var sum = 0;
var arr = [ 1, 2, 3, 4, 5 ];

$.each( arr, function( index, value ){
sum += value;
});

Javascript associative array

Javascript does not feature associative arrays. However, you can create something similar using object arrays.

Example:

arr={'key1': 'value1','key2':'value2'};

This array can be accessed with:

the_value = array['key2']

Values can be added or changes:

arr['key3']='value3';

More info here: http://www.i-programmer.info/programming/javascript/1441-javascript-data-structures-the-associative-array.html

You can iterate over the object array using Jquery:

$.each( arr, function( index, value ){
sum += value;
});

Tuesday, 5 August 2014

To get a mysql formatted datetime using javascript:

**
 * You first need to create a formatting function to pad numbers to two digits…
 **/
function twoDigits(d) {
    if(0 <= d && d < 10) return "0" + d.toString();
    if(-10 < d && d < 0) return "-0" + (-1*d).toString();
    return d.toString();
}

/**
 * …and then create the method to output the date string as desired.
 * Some people hate using prototypes this way, but if you are going
 * to apply this to more than one Date object, having it as a prototype
 * makes sense.
 **/
Date.prototype.toMysqlFormat = function() {
    return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};

I couldn't find a simpler jQuery solution to this.
If you want the time according to the user's time zone, remove the "UTC" in the second last line of the code.

Saturday, 31 August 2013

JQuery Source maps

JQuery Source maps allows javascript debugging even when using minified javascript.

http://jquerybyexample.blogspot.com/2013/01/all-you-need-to-know-about-jquery-source-maps.html

Friday, 26 July 2013

To submit a form using POST in jQuery (ajax), use the post method:
jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

http://api.jquery.com/jQuery.post/