Tatva-Artha

meaning of "it"

Voting fixtures in jQuery – Thumbs Up, Thumbs Down

without comments

Recently I had to implement voting system for one of my projects. In summary, the system had to allow user to easily vote Yay or Nay for a given item. I implemented this using thumbs-up thumbs-down images in jQuery/Ajax.

voting-in-jquery.png

First I wrote a re-usable function/plugin in jQuery:

$.fn.mouseOverImageSwitch = function(default_image, mouse_over_image) {
$(this).livequery('mouseout', function (){
$(this).attr('src',default_image);
});
$(this).livequery('mouseover', function (){
$(this).attr('src',mouse_over_image);
});
};

This function would allow me to call image-switching on mouse-over event on any element. Now we need to hook this call to an img element so we can switch images when the user brings mouse over a voting icon. Assuming, I had following elements with img elements with class attributes like “thumbs_up” and “thumbs_down”:

<a href="#"><img class="thumbs_up" src="/images/thumbs_up_grey.png"></a>
<a href="#"><img class="thumbs_down" src="/images/thumbs_down_grey.png"></a>

I did following to hook the call up.

<script type="text/javascript">
(function($) {
$(document).ready(function(){
$('.thumbs_up').mouseOverImageSwitch('/images/thumbs_up_grey.png','/images/thumbs_up_colored.png');
$('.thumbs_down').mouseOverImageSwitch('/images/thumbs_down_grey.png','/images/thumbs_down_colored.png');
});
</script>

In the original plugin function, I used livequery which allows me to bind mouse-over hook to elements that were added using ajax. I can image that too many such hooks would degrade performance. I will wait to see how this works before performing performance optimizations …

http://www.tatvartha.com/wp-content/plugins/sociofluid/images/digg_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/reddit_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/stumbleupon_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/delicious_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/google_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/twitter_16.png

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Written by admin

May 3rd, 2009 at 6:57 pm

Leave a Reply