Simple jQuery Expand/Contract Functionality
I can’t express enough how much learning jQuery has expanded my ability to simply and dynamically add amazing effects very easily to my User Interfaces. Maybe I should start a jQuery blog?
One feature I come across a lot is the ability to expand and contract a section of the page, say a <div>. Before when I wanted to do this, I had to resort to either adding markup to my code and importing huge function-specific script files (ie, all they did was perform the expand/contract) or rely on ASP.NET’s AjaxToolkit’s Collapsible Panel Extender. While these solutions… worked… I was never really comfortable using them, and they were hardly reusable or scalable. Enter jQuery.
Let’s say I have a bit of HTML that is like so:
<p class="toggle">Expand/Collapse</p>
<div>
<p>Some text goes here!</p>
</div>
<p class="toggle">Expand/Collapse</p>
<div>
<p>Some more text goes here!</p>
</div>
If you wanted the ability to show/hide that following div, the jQuery is ridiculously simple:
$(document).ready(function() {
$(".toggle").click(function () {
$(this).next("div").slideToggle("fast");
});
});
Yes, literally that’s it. So long as you keep with the HTML convention above, that’s all the jQuery you would need. You don’t have to add any extra markup or link one ID to another or anything. It’s all done via jQuery, and pretty much in one line ($(this).next("div").slideToggle("fast");).
Of course, sometimes you may want to do more. For instance, what if you wanted to change the text of the toggle button? Assume now that “Expand/Collapse” now reads “View Additional Info”. Here’s your jQuery:
$(document).ready(function() {
$(".toggle").toggle(function () {
$(this).text("Hide Additional Info")
$(this).next("div").slideDown("fast");
}, function() {
$(this).text("View Additional Info")
$(this).next("div").slideUp("fast");
});
});
Extremely simple, elegant, and even intuitive! If you ever doubted jQuery’s power before, you may want to reconsider. I sure did.
Share this:
Like this:
July 31, 2009 - Posted by Some.Net(Guy) | jquery, Tips & Tricks | expand/collapse, show/hide
3 Comments »
Leave a Reply Cancel reply
About
This blog is for nothing else than to try and help me help you and help you help me and us help everyone else try to fix anything and everything we can’t figure out with this language. I’ve got a pretty good handle on it as I’ve been doing it for several years, and can help with a lot, but I definitely don’t claim to know everything. That’s where you come in
If you’re interested in joining the discussion to post your own problems, fixes, etc. in an actual post, just email me or comment somewhere and we’ll talk about it.
Share this:
-
Recent Posts
-
Top Posts
-
Recent Comments
Portfolio
Resources
-
Stats
- 306,446 views since July 2007
Categories
Archives
- December 2009 (1)
- July 2009 (2)
- June 2009 (2)
- May 2009 (2)
- February 2009 (1)
- December 2008 (1)
- November 2008 (1)
- October 2008 (1)
- September 2008 (2)
- August 2008 (3)
- April 2008 (2)
- March 2008 (1)
- February 2008 (1)
- January 2008 (4)
- December 2007 (4)
- November 2007 (3)
- October 2007 (7)
- September 2007 (6)
- August 2007 (7)
- July 2007 (13)
RSS Feeds
RSS Feed
The code is broken,
$(document).ready(){ function() {
should be
$(document).ready({ function() {
and so on
@Cokeramirez –
Good catch! Your correction is almost right. It should be as it is now:
$(document).ready(function(){ … });
Sometimes even the dumbest of typos slips through the cracks :\
[...] you may well know from my previous few posts, I’ve been dipping my toes into the jQuery and Javascript world for quite some [...]
Pingback by Javascript: How To Implement Callback Functionality « .NET Discussion | December 21, 2009 |