_jQuery Accordion with 8 Lines of Code

Ok.  Maybe more than 8 lines of code but very little.  I have been working with another agency (that will rename nameless) on a mobile web site for a client of ours.  The agency provided the HTML/CSS/Images and Script for our technology team to implement.  We hook up all the back end and call it a day.

The section that came into question was an ‘accordion’ style UI for a set of FAQ’s and other content.  When I first loaded up their pages, everything worked fine.  Upon further inspection of their code I noticed a lot of JavaScript libraries that were not needed.  They included jQuery AND Prototype along with some effects and accordion scripts.  (Prototype alone is almost 100kb in file size and the other scripts were 42kb.  That still does not include the 70kb for jQuery)  I thought to myself, this is a mobile site…why have extra scripts if not needed.  To make matters even more funny, we are supporting Mobile Safari (iOS), Mobile Chrome (Android) and the BlackBerry Browser (RIM), it does not work in BlackBerry at all.  Forget enabling or disabling JavaScript it just does not work.  Even the elements are hidden.

So it got me thinking about the old saying, “if you want something done right you have to do it yourself”.  So I did.

I removed all the libraries except for jQuery and made all of the elements visible by default.  Basically if a user does not have JavaScript enabled then they can see everything.

Here is my HTML

<div>
     <a href="#">Really Great Information</a>
     <div class="accordion_content">
          Content that will expand
     </div>
     <a href="#">Really Great Information</a>
     <div class="accordion_content">
         Content that will expand again
     </div> 
</div>

Styles and other HTML can be wrapped around it. You can change the paragraph tags to div’s if needed but that is the structure you need more or less.

Now for the JavaScript.

Make sure you embed jQuery (I did from Google)

$('.accordion_content').hide();
//If JavaScript is not enabled, all content containers stay open, which is nice.

//Next, bind the click event to all of the targets, in this case all 'a' elements in the 'accordion_toggle' class
$('.accordion_toggle a').click(function(e){
     //code to come
});
//Within the click function we need to first check if the element being clicked is set as the 'current' which means open.  If it is, we need to remove the class 'current' and close the content container.
if($(this).parent().hasClass('current')) {
     $(this).parent()
          .removeClass('current')
          .next('.accordion_content').slideUp();
} else {
...
}

Then hide all open content containers:

If the element does not have the ‘current’ class then we need to first remove the ‘old’ ‘current’, close the ‘old’ ‘current’ and open and set the new ‘current’.

$(document).find('.current')
     .removeClass('current')
     .next('.accordion_content').slideUp();

$(this).parent()
     .addClass('current')
     .next('.accordion_content').slideDown();

Done.

Here is the final:

$(function() {
     $('.accordion_content').hide();
     $('.accordion_toggle a').click(function(e){
          if($(this).parent().hasClass('current')) {
               $(this).parent()
                   .removeClass('current')
                   .next('.accordion_content').slideUp();
          } else {
               $(document).find('.current')
                    .removeClass('current')
                    .next('.accordion_content').slideUp();
              $(this).parent()
                    .addClass('current')
                    .next('.accordion_content').slideDown();
          }
          e.preventDefault();
      });
});

Depending on how you format the code and what you count as a ‘line’ it can be about 8 lines :)

Demo here – View in Mobile Browser.  Desktop browser will work as well.  Works on iOS, Android, BlackBerry.

Mar
10
2011

_My Top 5 iOS Annoyances

  1. Add To Home Screen – When I browse to a web site using mobile safari I might choose to add it to my home screen since it can be a nice way to launch the web browser.  When I do that task, Apple (for some reason) decides to leave the Safari window and show me where the icon is being placed.  How nice…and annoying.  Now I have to go back to my Safari Icon and open it back up, which will now RELOAD the web page (in some cases)…argh!
  2. Push Notifications – Not sure if this is an iOS thing or just certain apps.  Let’s take ESPN Scorecenter for example, a score happens, I get a push notification alert sound.  I go to my phone and see the notification.  I then do anything and “poof” it goes away.  Why not open the app?  Why not give me an option to close or open the app?
  3. Auto Correct – Wow.  I really hate this one.  Why not an Auto Suggest?  I might want the incorrect word?  Why should I perform another action to NOT correct the word.  I should type away, it should ‘suggest’ a new word, if I want to use the word then I can initiate it otherwise ignore it.  Right now, if it suggests a word it uses it unless I tell it not to…which is slower…argh.
  4. Installation of Apps – similar to Add to Home Screen.  When I install an app, it leaves the app store to show me the icon and the installation process.  Wow…how exciting.  Why?  Makes no sense.
  5. Multitasking – It really is a joke.  Your telling me there is no way to quit an app unless I go to the multitasking screen?  Obviously you do not have kids.  They open up 20-30 apps and quit them all the time but they are still running?  Why not prompt for a ‘quit’ when you ‘quit’ an app?  C’mon.
Dec
23
2010

_STATUS: 404 NOT FOUND @ STORY WORLDWIDE

When I thought about writing a goodbye letter I figured I would do the usual and say thanks and good luck.  But to be honest it is more than that (thanks for the inspiration Martin).  It is more than thanks and good luck.  It is laughs, tears, wins, losses, thrives and struggles.

Eight years is a long time.  That is like going to high school twice (and times this place is like a high school).  From happy hours where Greg is having a strawberry eating contest, Todd eating full spoonfuls of Wasabi, Travis throwing up in creative (sorry Travis), Rica being carted down the mountain by the ski patrol, Cat, Stacy and Ben hi-jacking a school bus, Dave making all the PM’s cry, Mike and his hillbilly teeth, Chet break dancing in a suit of armor, American Idol style contest (does anyone still have that video of the Carrie rapping?), Softball and the Mojito Crew, summer parties at the beach, car arguments about BMW’s VS. Audi’s VS Fords, a little incident with a scooter and the chains at the Maritime Center, trips to Stratton and more.

Let’s also not forget the ones who have passed through these doors like Liz, Kai, Tai, Llama Lynn, Carol, Parchment, Tracy, Yury, Max and Flannigan, they were truly a pleasure :p

From Rimmel London to all the fun with the lovely ladies of COTY (JLo, SJP, Shania), the bottled water sites (Nestle) and all of their crazy contest that Stacy could dream up (Shrek / choose your own adventure! ARG), to some very serious clients like TradeTrak and Connolly Consulting, even K2.  We were ahead of the time with some of the video contest (glo girls & CK In2U) and fan competitions (ultimate fans) and even our own Stumble Upon (Perrier IER) and never forget the webisodes (obviously Spraychel).  We even did a mobile marketing app that took photos and uploaded them to a site that sent out emails back in 2005.  Here’s to the 4 AM launches of Acquisitions or e-Service, the 3 AM fun with the MLB contests, Sunday or even Saturday Midnight launches of contests, New Years Eve site launches and a few all nighters thrown in (shout out to Mark).   So many to list…

I am very proud to have been part of this company, we have done great work each and every year and I truly wish the best for everyone.

Special thanks to my team – Dave, Anthony, Tom, Travis, Steve and Jacek.  It has been has been a pleasure to work with all of you!  You all made “work” so much fun.

Thanks again.  PS – I won’t remove you any of you as my Facebook friends

joshdrew@gmail.com | facebook.com/jdrew | twitter.com/@jdruid | drew5.net

Sep
23
2010

_iPad App Reviews – Fun

These 3 are in the category of fun since they are sort of random. :)

ABC Player

Kudos for ABC on this one.  Real nice app.  The iPad just makes images pop and when you have some great images, it makes the app look even better.  That is what this has.  Awesome large images and previews of ABC shows.  Ability to watch quite a bit of past episodes, check scheduling and keep a history of videos watched gives this app a lot of positives.

Epicurious

For the aspiring cook, this app is for you.  It feels like the Betty Crocker cookbook.  Tabs on the side, big images, ingredient list, reviews and more.  Solid functioning app.  The one thing lacking is video.

GAP 1969 Stream

I like this app not because of the GAP or products, but because of an entirely new way to navigate.  The app gives off the impression that it is endless.  Keep scrolling left, right or up or down you will never come to the end.  It repeats, but it works as well.

Jun
09
2010

_iPad App Review – News

Today is the news category.

USA Today, Reuters, and Bloomberg

USA Today

USA Today takes a safe approach to their app.  It looks EXACTLY like their newspaper and functions like a web site.  At times you can get lost when navigating through articles.  The ads don’t function that well.  Orientation of the device does not provide that much enjoyment.  The app is ok.

Reuters

The Reuters app is a great news app.  Very clean.  It uses icons that we are familiar with (the dot notation for swipes).  Image gallery, pop up video and ease of getting back to all of the main articles.  They make use of orientation by moving the article to the right and giving you options to jump to other sections or directly into an article.  Interactive stock tickers give this app a little more life.  Overall, solid.

Bloomberg

Bloomberg took a different approach to colors, instead of white or light background they went black.  Not a fan.  Very little images if any, navigation is clunky, drop-downs seems like a band-aid to get to where you want to go.  Hopefully next release will be improved.

Jun
08
2010