Common utility methods of jQuery
jQuery provides several inbuilt utility methods. These utility methods uses $.namespace and they are very useful and handy for your routine programming tasks. I have put together some common jQuery Utility methods to make your programming routine easy and can save your valuable time.
- $.isArray(Object): Returns true if the object is an array.
//Code Starts var arrFirst = [50, 22, 10, 19, 22, 10]; var isArray = $.isArray(arrFirst); //Code Ends
- $.isEmptyObject(Object): Returns true if the object doesn’t define any methods or properties.
//Code Starts var arrFirst = [50, 22, 10, 19, 22, 10]; var isObject = $.isEmptyObject(arrFirst); //In this case, isObject will be false. //Code Ends
- $.isFunction(Object): Returns true if the object is a function.
//Code Starts function myFunc() { //function code } var isFunction = $.isFunction(myFunc); //Code Ends
- $.isNumeric(Object): Returns true if the object is a number.
//Code Starts var num = 1; var isNumeric = $.isNumeric(num); //Code Ends
- $.isWindow(Object): Returns true if the object is a Window. This method accepts an argument and determines whether the argument is a window. This is used in a number of places in jQuery to determine if we're operating against a browser window (such as the current window or an iframe).This returns true is object is window, otherwise false.
//Code Starts $(document).ready(function(){ alert($.isWindow(window)); var isFrame = $("#objFrm"); alert($.isWindow(isFrame)); }); //Code Ends
Here objFrm is defined as an iframe.
- $.isXMLDoc(Object): Returns true if the object is an XML document. Check to see if a DOM node is within an XML document (or is an XML document).
//Code Starts $(document).ready(function(){ $.isXMLDoc(document) // false $.isXMLDoc(document.body) // false }); //Code Ends
- $.type(Object): Returns the built-in JavaScript type for the object.
//Code Starts $(document).ready(function(){ var iVal = 12; $.type(iVal); //number iVal = 'jQuery'; $.type(iVal); //string }); //Code Ends
Below is the list of types which can be returned by this method based on the value.
- boolean
- number
- string
- function
- array
- date
- regexp
- null
- undefined
- object
- $.trim(Object): This method removes white spaces from beginning and end of the string.
//Code Starts $(document).ready(function(){ var str = ' jquery by example '; str = $.trim(str); }); //Code Ends
Please bookmark this article and make a practice to use these method as they can save your time.
Feel free to contact me for any help related to jQuery, I will gladly help you.