jQuery – Calling Your Own Functions

color-02 While jQuery has a lot of functionality built into it, you may find that you have a need to implement your own functions and attach them to the jQuery framework in some way.

There are two ways to do this. You could extend the framework by creating a plug-in or you could use one of the built-in functions and simply tell it to call your function.  For one-off implementations, using the function is easier and recommended.  But if you are going to reuse the code between multiple projects you’ll want to implement the added functionality as a plug-in.

Today, we’ll look at using the built-in function.

The method we will be using for calling our own functions is the .each() method.  Each takes a function pointer that accepts a zero based index into the list we are each()ing over.

The other thing you need to know is that the selector $(this) represents the current item you are iterating over.  In practice, the index parameter gets ignored and we use $(this) almost exclusively.

Finally, since this is a one-off implementation, you will probably just use an anonymous function to implement your each so that your code ends up looking something like this:

$('selector').each(function(index) {
    $(this).doSomethingHere();
});

 


Other post in jQuery

Other Related Items:

Vanity Fair's Presidential Profiles: Defining Portraits, Deeds, and Misdeeds of 43 Notable Americans--And What Each One Really Thought About His PredecessorVanity Fair's Presidential Profiles: Defining Portraits, Deeds, and Misdeeds of 43 Notable Americans--And What Each One Really Thought About His Predecessor

Forty-three men have held the highest office in the United States, making up an exclusive club of statesmen and s... Read More >

Super Soft Luxury Cashmere Feel Fringe Scarf - (Choose from 33 fashion colors / patterns)Super Soft Luxury Cashmere Feel Fringe Scarf - (Choose from 33 fashion colors / patterns)Experience the soft luxurious feel of cashmere feel fashion neckwear. Get the look and feel of high end scarves at an affordable price. Choose from ou... Read More >
No Pocket Sweatpants Heavy Blend 7.75 oz. by Gildan (Style# 18200)No Pocket Sweatpants Heavy Blend 7.75 oz. by Gildan (Style# 18200)50% cotton, 50% polyester, 7.75 oz. Drawstring waist, no pockets, elastic cuffs.

Related Post

One Response to “jQuery – Calling Your Own Functions”

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor