Capturing the expand and collapse event on a jQuery Mobile collapsible set

Filed under: jQuery, jQuery Mobile

comments (2) Views: 10,528

This morning over at Goba.mobi I needed to capture the expanding/collapsing of a jQuery Mobile collapsible set. I wanted to add a class to an element, but only when a a specific set was opened. I Googled for a few minutes before finding a post over on the jQuery Mobile forums. Turns out it's pretty simple. Here's what I did.

In our case we're using form tags as the container, so that's what I bound my event to. I only cared about the expand event since I wanted to target the opening of a specific container, namely the Phone section. Next I used simple JavaScript property access (this.id) to check the id if the current target. Finally I added and removed a class as needed.

Pro-tip: Rather than applying a large number of attributes to an element with jQuery's css() method, consider creating a class for those styles. Then using the addClass()/removeClass() partner methods to add and remove the styling. Much simpler and it keeps your CSS out of your JavaScript code.

Amazon logo

If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!

comments powered by Disqus
coach outlet online jordan 13 grey toe beats by dre cyber monday michael kors black friday beats by dre cyber monday jordan 6 black infrared north face cyber monday michael kors cyber monday north face black friday coach outlet black infrared 23 13s north face cyber monday jordan 6 black infrared north face black friday coach cyber monday jordan 11 legend blue north face cyber monday black infrared 6s lebron 12 north face black friday jordan 11 legend blue louis vuitton outlet jordan 13 grey toe grey toe 13s beats by dre black friday coach black friday jordan 13 grey toe coach cyber monday uggs black friday jordan 13 black infrared 23 uggs cyber monday barons 13s uggs black friday beats by dre cyber monday black infrared 6s jordan 13 bred jordan 13 black infrared 23 north face black friday black infrared 6s jordan 11 legend blue michael kors black friday jordan 13 grey toe coach black friday michael kors black friday michael kors cyber monday beats by dre cyber Monday north face cyber monday coach black friday michael kors cyber monday beats by dre cyber Monday north face black friday beats by dre black friday lululemon black friday uggs black friday jordan 13 bred coach cyber monday beats by dre black friday uggs black friday coach black friday black infrared 6s