{"id":797,"date":"2010-06-04T16:27:05","date_gmt":"2010-06-04T10:57:05","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=797"},"modified":"2016-12-19T15:04:56","modified_gmt":"2016-12-19T09:34:56","slug":"jquery-chaining-of-your-custom-function-calls","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/jquery-chaining-of-your-custom-function-calls\/","title":{"rendered":"jQuery : Chaining of your custom function calls"},"content":{"rendered":"<p>Hi Friends,<\/p>\n<p>I always used to think how jQuery provides the chaining mechanism, must be something really complex so never cared to find it out. Though there was a desire to to use the same concept in my own custom javascript functions. Recently I realised how easy it is, so thought would share with you with the help of an example.<\/p>\n<blockquote>\n<pre lang=\"javascript\">\r\njQuery.fn.getStudents= function(count) {  \/\/count is the parameter name and getStudents is the function name.\r\n\/\/write your code here\r\n...\r\nreturn myObject  \/\/return is optional\r\n});<\/pre>\n<\/blockquote>\n<p>Now we can access the above function using dot operator on jQuery Object.<\/p>\n<blockquote>\n<pre lang=\"javascript\">jQuery(\"#myListId\").getStudents(5);<\/pre>\n<\/blockquote>\n<p>Please make a note the object returned by the previous function is made available to the chained functions as &#8220;this&#8221; object, that is one of the reason chaining became so useful. So in the above function getStudents(), &#8220;this&#8221; object would refer to the object returned by jQuery(&#8220;#myListId&#8221;). <\/p>\n<p>Remember, to have chaining upto multiple levels, it becomes the obligation for the calling function to must return an object\/value to the chained function being called on it.<\/p>\n<blockquote>\n<pre lang=\"javascript\">jQuery.fn.highlightNames= function() {\r\n\/\/write your code here\r\njQuery(this).find(...);\r\n...\r\n});<\/pre>\n<\/blockquote>\n<p>Now we can access the above function i.e highlightNames() using dot operator even on another function.<\/p>\n<blockquote>\n<pre lang=\"javascript\">jQuery(\"#myListId\").getStudents(5).highlightNames();<\/pre>\n<\/blockquote>\n<p>Here I didn&#8217;t have to passes the students object to highlightNames, as it was available as a &#8220;this&#8221; object.<\/p>\n<p>Hope it helped!<\/p>\n<p>~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nhttp:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi Friends, I always used to think how jQuery provides the chaining mechanism, must be something really complex so never cared to find it out. Though there was a desire to to use the same concept in my own custom javascript functions. Recently I realised how easy it is, so thought would share with you [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1],"tags":[247,27,246],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/797"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=797"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/797\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}