Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Wednesday, 11 September 2013

create own support Ctrl+v, Ctrl+c, Ctrl+X and 'Numeric only' jquery plugin

(function ($) {
    $.fn.forceNumeric = function () {
        return this.each(function () {
            // bind handler of 'paste' event
            $(this).bind(
                {
                    paste: function (e) {
                        var content = e.originalEvent.clipboardData.getData('text/plain');
                        var regExp = /^\d+$/;
                        if (!regExp.test($.trim(content))) {
                            e.preventDefault();
                        }
                    }
                });
            $(this).keydown(function (e) {
                var key = e.which || e.keyCode;

                if (!e.shiftKey && !e.altKey) {
                    if ((key >= 48 && key <= 57 ||
                        // Numeric keypad
                        key >= 96 && key <= 105 ||
                        // comma, period and minus, . on keypad
                        key == 190 || key == 188 || key == 109 || key == 110 ||
                        // Backspace and Tab and Enter
                        key == 8 || key == 9 || key == 13 ||
                        // Home and End
                        key == 35 || key == 36 ||
                        // left and right arrows
                        key == 37 || key == 39 ||
                        // Del and Ins
                        key == 46 || key == 45) && !e.ctrlKey) {
                        return true;
                    }

                    // Allow: Ctrl+C, Ctrl+V, Ctrl+X
                    if ((e.keyCode == 67 || e.keyCode == 86 || e.keyCode == 88) &&
                        (e.ctrlKey === true || e.metaKey === true)) {
                        return true;
                    }
                }

                return e.preventDefault();
            });
        });
    };
}(jQuery));

Monday, 5 December 2011

static variable in javascript

Functions are not primitive values in JavaScript, but a specialized kind of object, which means that functions can have properties. When a function needs a “static” variable whose value persists across invocations, it is often convenient to use a property of the function, instead of cluttering up the namespace by defining a global variable. You could store this information in a global variable, but that is unnecessary, because the information is used only by the function itself. It is better to store the information in a property of the Function object. So there're two solutions to solve this.
1) using global variable
2) function properties
function uniqueSuffix() {
    uniqueSuffix.counter = uniqueSuffix.counter || 0;
    return uniqueSuffix.counter++;
}

uniqueSuffix();
uniqueSuffix();
console.log(uniqueSuffix.counter); // >>2

Tuesday, 29 November 2011

add remove() function to array of javascript

Array.prototype.indexOf = function(e){
    for (var i = 0; i < this.length; i++) {
        if (e==this[i]) {
            return i;
        }
    }

  return -1;
}

Array.prototype.remove = function(elem) {
    var match = -1;
    while ((match = this.indexOf(elem)) > -1) {
        this.splice(match, 1);
    }
};

How to create an unique array using javascript

Array.prototype.indexOf = function(e){
    for (var i = 0; i < this.length; i++) {
        if (e==this[i]) {
            return i;
        }
    }

  return -1;
}

Array.prototype.unique = function () {
    var values = this;
    var uniqueValues = [];
    for (var i = values.length; i--;) {
        var val = values[i];
        if (uniqueValues.indexOf(val) === -1) {
            uniqueValues.unshift(val);
        }
    }
    return uniqueValues;
};

Friday, 25 November 2011

how to customize icon for JQuery-UI buttons

Step1: define css



Step2: