Simple Show & Hide with Mootools

Thursday, April 2, 2009 3:48
Posted in category Mootools, Tutorial

I see few of my colleagues struggling through long codes, just to implement simple show and  hide. Yes this can be done with simple javascript code, but the implementation is repetitive and cumbersome. Show and hide is used in almost every web site here and there. So this post should be helpful to those people who really want a simple solution to this simple yet tedious problem! ;)

Mootools is a gem of javascript libraries. And implementing a simple show hide is even more simpler with mootools. This code snippet can be used anywhere within a webpage to implement a simple Show Hide. Here is the code

window.addEvent(‘domready’,function(){

Element.extend(     //Implement the following functions in every element existent in the webpage
        {
            show:function(e){
          
            this.setStyle(‘display’,'block’);
          
            },
            hide:function(e){  
            this.setStyle(‘display’,'none’);
          
            }
        }
        );

});

This mootools snippet implements the custom method show and hide in every element of the webpage. This means we can use the simple $(‘element’).hide(); and $(‘element’).show(); to show and hide the element.  (A piece of cake, isn’t it?)

The above code snippet should be written in the script section ;) yes you know it!!

The remaining html implementation is as below

<span onclick=”$(’stage’).hide();”>Hide</span>

<span onclick=”$(’stage’).show();”>show</span>

<div id=”stage”>

This is the text to be hidden or shown

This is the text to be hidden or shown

This is the text to be hidden or shown

This is the text to be hidden or shown

</div>

The html section shows the general usage of the show hide method we just implemented in every element. This code is handy! just copy and use it in your pages. Yes, you can copy!

I will soon maintain a demo site to see all the codes in action.
Till then happy reading ;)

VN:F [1.8.8_1072]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.8_1072]
Rating: +2 (from 2 votes)
http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/digg_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/reddit_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/stumbleupon_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/delicious_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/blogmarks_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/google_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/myspace_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/facebook_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/yahoobuzz_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/twitter_32.png
You can leave a response, or trackback from your own site.

Leave a Reply