.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

My first jQuery plug-in

I’ve been using jQuery for quite some time and eventually after enough time you will end up writing your own custom plug-in. Fortunately, the mechanics of writing a jQuery plug-in is rather simple. Perhaps that is why we see so many plug-ins for jQuery. The jQuery site alone has some 1500+ of them.

Recently, I decided to redo my site using ASP.NET MVC and decided to use rounded corners for the resume module. I found an excellent CSS implementation and so realized that surrounding existing tag with the necessary mark-up to implement rounded corners was cumbersome and problematic. Custom jQuery plug-in to the rescue.

The CSS rounded corner implementation can be found at the following site http://www.schillmania.com/projects/dialog/png/ Basically, the technique wraps the following mark-up around the desired content you want add rounded corners to.

<div class="rounded-corners">
  <div class="hd"><div class="c"></div></div>
  <div class="bd">
    <div class="c">
      <div class="s">
         <!-- Your content goes here -->
      </div>
    </div>
  </div>
  <div class="ft"><div class="c"></div></div>
</div>

Basically, this mark-up along with some CSS and images will render rounded corner container around your content. This works fine for the odd content that you want rounded corners for. But what if you have dozens of places where you want rounded corners? Do you really want to pollute the mark-up with all those div tags. In my case I decided that a jQuery plug-in would solve the problem. Rather than manually add all the above div tags why not write a jQuery plug-in that will wrap an element with the rounded corner div tags. The jQuery call would look like $(selector).roundedCorners(); This plug-in would take the DOM elements that are matched by the selector and dynamically add these div tags.

Creating a jQuery plug-in is as simple as creating a function;

$.fn.roundeCorners = function() {
  //your code goes here
}

One problem with this approach is it assumes that $ is an alias for jQuery. What if a call is made to $.noConflict(); which removes the $ alias. You could use the jQuery function directly jQuery programmers are so use to to using the $ alias. To get around this the following code is used

(function($) {
    $.fn.roundeCorners = function() {
        //your code goes here
    }
})(jQuery);

This syntax is difficult to get your head around but what it does is call an anonymous function with a parameter $ and passes jQuery to the function, that is, for the duration of the function $ represents the function jQuery.

That’s all there is to creating a jQuery plug-in. Now let’s write a jQuery plug-in that dynamically adds the rounded corner div tags.

    1 (function($) {

    2 

    3     $.fn.roundedCorners = function() {

    4 

    5         //wrap the element with div tags required to round the corners

    6        $(this)

    7       .wrap("<div class='rounded-corners'></div>")

    8       .wrap("<div class='rounded-corners-bd'></div>")

    9       .wrap("<div class='rounded-corners-c'></div>")

   10       .wrap("<div class='rounded-corners-s'></div>");

   11 

   12        //need to add header and footer div tags before and after the body div tag

   13        var selector = '.rounded-corners ' + this.selector;

   14        var bd = $(selector).parents('.rounded-corners-bd');

   15        $("<div class='rounded-corners-hd'><div class='rounded-corners-c'></div></div>").insertBefore(bd);

   16        $("<div class='rounded-corners-ft'><div class='rounded-corners-c'></div></div>").insertAfter(bd);

   17 

   18        return this;

   19     }

   20 

   21 })(jQuery);

The above code wraps all matched DOM elements with the rounded corner div tags.  The following shows how to make use of this plug-in

    5 <script type="text/javascript">

    6   $(function() {

    7     $('#foo").roundedCorners();

    8     $('.moo").roundedCorners();

    9   });

   10 </script>

   11 

   12 <div id="foo">Hello foo</div>

   13 

   14 <div class="moo">Hello moo 1</div>

   15 

   16 <div class="moo">Hello moo 2</div>

All three div tags will be wrapped with the rounded corner div tags and will be rendered with rounded corners.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by CynotWhyNot on Wednesday, August 05, 2009 8:18 AM
Permalink | Comments (12) | Post RSSRSS comment feed

Related posts

Comments

discount designer handbags US

Friday, January 01, 2010 10:59 AM

discount designer handbags

Your blog seems interesting.Regards,Kevin.

designer handbags US

Tuesday, January 12, 2010 11:45 AM

designer handbags

Today, the designer handbags have affected not only those of the lower class fashion, but also those who are financially well in the street. This may be a consequence of the sale of beautiful designer bags is not a profitable business in discount handbags online store in bagsvendor.com. But this is not necessarily true, because these Louis Vuitton designer handbags can be purchased at a discounted price! If you try to buy and sell parts, you realize it is worth their investment for yourself. Why? Because these discount bags are durable, can last longer and are really cheap! Yes, Louis Vuitton bags can be bought at low cost and can also be purchased online from a wholesaler or supplier, such as bagsvendor.com. It is just a store that sells bags directly from manufacturers and therefore the lowest price possible. If this interests you, the bagsvendor can be identified in the google or any other online directory.

designer handbags US

Wednesday, January 13, 2010 1:13 AM

designer handbags

A good way to buy a pair of Supra shoes is searching your Supra shoes in winnersneaker.com. Initial searching in the supra shoes online store, after the selling season, can find the discount supra shoes by many supra shoes fans. Looking for an offer like this is more likely to buy a pair of this unique one, rather than a pair of worse ones. Whenever you have a pair of Supra Skytop Shoes, we must pay attention to returns policy in the winnersneaker.com. I do not want to be stuck with a pair of Supra Suprano High shoes that you are not satisfied, because they are able to return or receive a refund for you. It is your hard earned money, you should do some research before you spend. Supra shoes also have a wide range of sizes in different styles and colors. Supra Suprano Low shoes and Supra Low shoes were designed and remarkably interesting tone and texture with many shoes collector. Most departures are in shades of gray, white, red and black. Even if you can find the red and green supra shoes to everyone. Supra Skytop shoes checkered shoe design is a complete form of positive attention with a combination of red, white and black colors. The sneaker and the shoe are full range with a wide variety of styles to choose from. The winter shoes with a full collection of jackets and Knitwear Designs are apparently in perfect sync with the images in winnersneaker.com. When it comes to the winter collection can not simply ignore the final assembly and original supra shoes online store. The most common supra shoes are colored black, blue and gray are well adapted to the demand for all generations. Find more supra shoes in winnersneaker.com.

supra shoes US

Wednesday, January 13, 2010 12:07 PM

supra shoes

A good way to buy a pair of Supra shoes is searching your Supra shoes in winnersneaker.com. Initial searching in the supra shoes online store, after the selling season, can find the discount supra shoes by many supra shoes fans. Looking for an offer like this is more likely to buy a pair of this unique one, rather than a pair of worse ones. Whenever you have a pair of Supra Skytop Shoes, we must pay attention to returns policy in the winnersneaker.com. I do not want to be stuck with a pair of Supra Suprano High shoes that you are not satisfied, because they are able to return or receive a refund for you. It is your hard earned money, you should do some research before you spend. Supra shoes also have a wide range of sizes in different styles and colors. Supra Suprano Low shoes and Supra Low shoes were designed and remarkably interesting tone and texture with many shoes collector. Most departures are in shades of gray, white, red and black. Even if you can find the red and green supra shoes to everyone. Supra Skytop shoes checkered shoe design is a complete form of positive attention with a combination of red, white and black colors. The sneaker and the shoe are full range with a wide variety of styles to choose from. The winter shoes with a full collection of jackets and Knitwear Designs are apparently in perfect sync with the images in winnersneaker.com. When it comes to the winter collection can not simply ignore the final assembly and original supra shoes online store. The most common supra shoes are colored black, blue and gray are well adapted to the demand for all generations. Find more supra shoes in winnersneaker.com.

discount chanel bags US

Thursday, January 14, 2010 12:42 AM

discount chanel bags

Discount Chanel Bags on big sale, this may be a big news in the Internet. But you will find that it is in discount price, wont you? Yes, this is my answer. All of us like discount chanel bags, including you. Even if a woman goes to purchase a very nice bag, even when they have to travel to many online stores(bagsvendor.com is just one of them) to find the ideal starting point for your needs with your personal clothing. Chanel bags have always believed that the stock market may not be the prettiest for many ladies. There must be another market in the online discount designer handbags store. Just take a look at the designer bag-Chanel Handbags online store available, just designer handbags on sale. Style by adopting the technique is made and, importantly, the prestige that come together for almost everyone is certainly wonderfully unique. Discount chanel bags is a opinion in our outlet and our onlie store. But in the meantime, these bags are extremely expensive. If youre an average American like me, you think at least three times before making purchases. Finding discount chanel bags in bagsvendor.com is a normal fashion bags purchase in many womens lives.

discount designer bags US

Sunday, March 07, 2010 4:04 PM

discount designer bags

Funny, I actually had this on my mind a few days ago and now I come across your blog.

designer handbag US

Monday, March 08, 2010 10:54 PM

designer handbag

Substantially, the article is in reality the sweetest on this precious topic. I harmonise with your conclusions and will thirstily look forward to your upcoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay abreast of any updates. Pleasant work and much success in your business efforts!

designer handbag US

Tuesday, March 09, 2010 2:34 PM

designer handbag

Substantially, the article is in reality the sweetest on this precious topic. I harmonise with your conclusions and will thirstily look forward to your upcoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay abreast of any updates. Pleasant work and much success in your business efforts!

nba stars shoes US

Wednesday, March 10, 2010 9:45 PM

nba stars shoes

Hey, guy, your blog is nice. It can bring me many useful information.

worldwide wholesale shoes US

Wednesday, March 10, 2010 11:28 PM

worldwide wholesale shoes

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

designer handbags US

Thursday, March 11, 2010 2:48 AM

designer handbags

You did the a great work writing and revealing the hidden beneficial features of BlogEngine.Net, that I found it was popularly used by bloggers nowadays. I think BE has emerged to be one of the best blogging platform right now. I wish you good luck with your blogging experiences.

lebron james shoes US

Thursday, March 11, 2010 6:47 AM

lebron james shoes

Your blog seems interesting.Regards,Kevin.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Thursday, March 11, 2010 9:50 PM