• Home
  • About
  • Contact Us
  • Gallery
  • Video
  • Archives
  • Site Map
Grab the RSS feed

FlashBannerOnline

FlashBannerOnline
  • Home
  • FlashBannerOnline
  • Free Downloads
  • General
  • Tutorials
    • Adobe AIR
    • CSS
    • Fireworks
    • Flash
      • ActionScript
    • FLEX
    • Javascript
      • JQuery
      • MooTools
    • PHP

Simple JQuery Expandable Content Box

  • Javascript, JQuery, Tutorials

    Posted on October 15th, 2009

    Written by Behrouz Pooladrag

    Related Posts

    • Creating a Sliding Menu by MooTools Framework
    • Happy New Year 2011
    • printf function for ActionScript3

    Tags

    animate banner with jquery, banner in jquery, banner jquery, banner jquery expandi lateral, Content Box, cs5 expandable banner, diagonal expand div jquery, expanable jquery and flash, expand banner en flash et jquery, expand banner en jquery, expand jquery banner, expand jquery lateral, Expandable, expandable banner as3 examples, expandable banner flash tutorial, expandable banner javascript, expandable banner jquery, expandable banner tutorial banner with jquery, expandable banners jquery, expandable banners tutorial html javascript, expandable content box with jscript, expandable context box with jquery, expandable flash banner, expandable flash banner tutorial, expandable flash on cs5, expandable list with content box, expandable text box in php, expanding banner jquery, expanding banner on the left with jquery, expanding banners jquery, expando banner in jquery, expando banner in jquery tutorial, extendable banner jquery, flash *rollover banner* examples, flash - expandable banners tutorial, flash as3 flash container div, flash banner expand page with javascript, flash banner tutorial, flash expandable banner swfobject tutorial, flash expandable banner tutorial, flash expanding banner tutorial, html content inside expandable a box, html expandable banner tutorials, Javascript, javascript expandable box, JQuery, jquery banner, jquery banner ad expand, jquery banner blinds expanding mouseover, jquery banner diagonal expand, jquery banner example, jquery banner expandable, jquery banner websites, jquery collapsible banners, jquery copy text with url, jquery css comment simple, jquery expandable banner, jquery expandable banner ad similar to iad, jquery expandable banners, jquery expandable box, jquery expandable content, jquery expandable div, jquery expanding banner, jquery expanding banner ad, jquery expanding banner image, jquery expanding banners, jquery expanding header ad, jquery map location rollover, jquery overlay banner, jquery resizable auto expanding text box, jquery rollover banner, jquery rollover location map caption out, jquery simple expandable, jquery simple expanding div over page, map rollover jquery content, overlay banner jquery, show hide toggle javascript that popouts a div, tutorial flash cs5 expandable banner, web
    Simple JQuery Expandable Content Box

    Just recently I have had to finally bite the bullet and learn some proper Javascript. Not just making text in input boxes disappear when the user clicks on them, I’m talking PROPER stuff like making boxes appear and disappear inside already existing content.

    This tutorial is written for JQuery but do not think that I would recommend JQuery over other frameworks such as MooTools. I personally like using JQuery as the CSS style traversing of the DOM is more intuitive to me, however I would recommend that you take a look and try all you can, you may prefer the style used by the other frameworks. To me MooTools came across as horribly complicated hence this tutorial is for JQuery.

    What does this tutorial do?

    It shows you how to make a preloaded content box appear and disappear when the user clicks a “View More” link. If you want a better idea check the demo page I have created to go with this. It has the code included but I will also post it here. First off don’t forget to include the JQuery library, which you can download from the JQuery site.

    THE HTML CODING

    <html>
    <head>
    <title>Simple JQuery Expanding Content</title>
    <style type="text/css">
    .outer-box {
    width: 100%;
    height: auto;
    float: left;
    }
    .inner1 {
    width: 98%;
    border: 1px solid #ff0000;
    }
    
    .inner2 {
    width: 98%;
    border: 1px solid #ffff00;
    }
    </style>
    <script type="text/javascript" src="link/to/jquery/library"></script>
    </head>
    <body>
    <div>
    <div>
    <p>some content</p>
    </div>
    <div>
    <p>Some extra content</p>
    </div>
    <div>
    <p><a href="" title="View More">View More</a></p>
    </div>
    </div>
    </body>
    </html>
    

    THE FINISHED CODE

    $(".vm").toggle(function(){
    $(this).parents("div").prev(".inner2").show('slow');
    $(this).text("View Less");
    $(this).attr("title", "View Less");
    },function(){
    $(this).parents("div").prev(".inner2").hide();
    $(this).text("View More");
    $(this).attr("title", "View More");
    });
    

    Now if you’ve not done any Javascript before this probably looks like it did to me a pile of strangeness! Honestly though it is simple enough to understand.

    If you’re just reading this for the code and don’t want to understand then just copy and paste it and run away. That’s it go on you free loader! Ctrl+C, Ctrl+V and runnnnnnnnnn! Don’t leave a comment or anything nooooo just run away!

    Sorry…

    Ok, now to explain the code…if there’s anyone left.

    First thing to explain is the very first line.

    $(function(){
    });
    

    This is the same as typing

    $(document).ready(function(){
    //some javascript stuff here
    });
    

    If you don’t know the basics of JQuery this line ensures that the document is ready to be altered by your Javascript. There are reasons for this but I won’t be explaining them here. If you want to know why check the JQuery site, in particular the getting started tutorials.

    As you can see by comparing the HTML and Javascript you should be able to spot that there is one containing box (class outer-box) are two boxes inside that container (classes inner1 and inner2), there is also one final div which contains the “View More” link. Your HTML does not need to resemble this but you will need to change any references to my classes and you may also have to alter some of the JQuery itself to make sure you are selecting the right divs.

    After checking the document is ready to be used we hide the div containing the extra information which we want to show/hide.

    $(".inner2").hide();
    

    English translation:- Find something in the document with the class of inner2 and use the hide function with no parameters. This will remove it from view.

    Once that is done I set up the event which fires off the code to show or hide the information.

    $(".vm").toggle(function(){
    
    },function(){
    
    });
    

    Right that looks a little more complicated than the last bit, lets check the English translation:-

    If the user clicks anything with a class of vm (in this case our View More link) toggle between the next two functions. Now that sounds more reasonable doesn’t it? So I click it once it fires the first function, I click it again it fires the second. Easy!

    OK Now I have two toggle states set up which will fire one after the other given clicks of the View More link. But what do I want it to do? Well if the information is visible I need to hide it and if it’s hidden I need to show it. As I know I have already hidden the content this makes things alot easier.

    Our toggle code now becomes:-

    $(".vm").toggle(function(){
    $(this).parents("div").prev(".inner2").show('slow');
    },function(){
    
    });
    

    English translation:- From the element which has fired this code, go up to it’s parent element and find anything with the class inner2 which is previous to where we are and apply the show function using the inbuilt parameter slow. This of course shows the element we wish to show. To hide it again we simple do the opposite.

    $(".vm").toggle(function(){
    $(this).parents("div").prev(".inner2").show('slow');
    },function(){
    $(this).parents("div").prev(".inner2").hide();
    });
    

    The observant amongst you will have noticed there is no nice styling to the hide. This is a usability concession, why would the user want it to gracefully disappear off of the screen, they clicked to get rid of it, they want it gone and gone yesterday!

    The rest of the code is me being my OCD self. I don’t like the thought of someone clicking a link that says “View More” seeing the more pop out infront of them but still having the link say “View More” it should be “View Less” or words to that effect.

    $(".vm").toggle(function(){
    $(this).parents("div").prev(".inner2").show('slow');
    $(this).text("View Less");
    },function(){
    $(this).parents("div").prev(".inner2").hide();
    $(this).text("View More");
    });
    

    Also you can’t very well claim to be accesible if you don’t sort out the title for the link as well!

    $(".vm").toggle(function(){
    $(this).parents("div").prev(".inner2").show('slow');
    $(this).text("View Less");
    $(this).attr("title", "View Less");
    },function(){
    $(this).parents("div").prev(".inner2").hide();
    $(this).text("View More");
    $(this).attr("title", "View More");
    });
    

    The attr notation allows you to change almost any attribute of an HTML element.

    Suprisingly enough that is it! If you load up your page and you can see the content which is supposed to be hidden there is something wrong with your code. Go back and make sure have all the brackets in the right place and all the semi-colons.

    As written this code will also work for multiple elements such as a list of portfolio sites. As long as the class names stay the same it will work just fine.

    I hope this is helpful to anyone, while I was researching how to do this there was no one tutorial which showed you this in it’s entirety…but now there is!!

    This entry was posted on Thursday, October 15th, 2009 at 8:10 am and is filed under Javascript, JQuery, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    You might also like

    Creating a Sliding Menu by MooTools Framework MooTools is an object oriented framework created in JavaScript that could be used to create a rich interactive...
    Edit the CSS styles of an element in Javascript I’ve actually started to learn the Internet beast JavaScript, and I think it’s about time I shared some knowledge...
    Date Class in AS3 This tutorial will teach you how to use the Date Class in AS3 to retrieve all information related to time (includes...
    Run Applications from a Flash (AS3) Projector EXE So i’ve been writing some CD launchers recently with some handy tools on. One thing i had major trouble with...
  • 0 Comments

    Take a look at some of the responses we've had to this article.

  • Post a Comment

    Let us know what you thought.

  • Name:

    Email (required):

    Website:

    Message:

  • Ad Ad

  • FlashBannerOnline.Com

  • Subscribe

    Enter your email address:

    | More

  • Blogroll
    • Random Flash Banner!
  • Tag Cloud

    • actionscript addChild addEventListener Adobe AIR Air Animation AS2 AS3 as3 preloader Banner beginFill BitmapData blog.flashbanneronline.com Class CSS Date Class draw Flash flash.net.URLLoader flash banner free flash banner generator flash banner happy new year flashbanneronline FlashBannerOnline.com flash banner online free flash player function game getChildIndex Javascript lightbox Loading Mysql new year flash banner Object Optimization package PHP Sprite tot Tweener URLLoader URLRequest web XML
  • Recent Comment
    • Behrouz Pooladrag on Flash Optimization – Freezing And Unfreezing Objects
    • Valerie on Flash Optimization – Freezing And Unfreezing Objects
    • FlashLord on RSS Reader for FlashBannerOnline Blog!
    • Behrouz Pooladrag on Using ActionScript 3.0 with PHP Loading External Variables
    • LUN on Using ActionScript 3.0 with PHP Loading External Variables
  • RSS FlashBannerOnline
    • Happy New Year 2012 & Gift!
    • New Templates Oval Light Added to Flash Banner Online
    • FlashBannerOnline started in Arabic language
    • Happy New Year 2011
    • New Templates Christmas 2011 Added at Christmas!
    • New Templates angel animate Added to Flash Banner Online
    • Added Tow New Preloader (Pro Preloader , Rotate Preloader)
    • Happy New Year , by new Theme (Christmas 2010)
  • Add to Technorati Favorites

    Subscribe
  • Google
    Custom Search

Copyright © 2009 FlashBannerOnline - Powered by Wordpress.

FBO by