Ask Ben: Displaying The Next Hidden Element Using jQuery (Update)

Posted January 28, 2009 at 3:15 PM

Tags: Javascript / DHTML, Ask Ben

This is just a quick update to my previous post on iterating over an array in jQuery. After I posted the solution this morning, it was mentioned that we might want to update it to allow visible elements to be hidden and then re-shown via the button click. The update to this is small and actually simplifies the code - rather than keeping an internal index of the next item to be shown, we are simply going to show the first hidden element in our jquery stack:

 
 
 
 
 
 
 
 
 
 

The updated code is as follows:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>jQuery Data Demo With Iterative Click</title>
  •  
  • <script type="text/javascript" src="jquery-1.3.1.pack.js"></script>
  • <script type="text/javascript">
  •  
  • // When the document loads, initialize the button.
  • $(
  • function(){
  • var jButton = $( "button" );
  •  
  • // First, hide all the list items.
  • $( "li" ).hide();
  •  
  • // Hook up the "close" links.
  • $( "li a" )
  • .attr( "href", "javascript:void( 0 )" )
  • .click(
  • function(){
  • $( this ).parent().slideUp();
  • }
  • )
  • ;
  •  
  •  
  • // Now, we are going to bind data to the button.
  • // Since we want to be able to add/remove the
  • // elements, we are not going to deal with an
  • // index. Rather, we are gonna go by the first
  • // hidden element in the collection.
  • jButton.data(
  • "config",
  • {
  • Collection: $( "li" )
  • }
  • );
  •  
  • // Now that we have our data bound, let's bind
  • // the click event.
  • jButton.click(
  • function( objEvent ){
  • var jThis = $( this );
  •  
  • // Get the config data out of the button.
  • var objConfig = jThis.data( "config" );
  •  
  • // Find the first hidden element in the
  • // jQuery stack.
  • var jHidden = objConfig.Collection.filter( ":hidden:first" );
  •  
  • // Try to show the hidden one (if none
  • // were returned, this will simply get
  • // passed over.
  • jHidden.slideDown();
  •  
  • // Prevent default event (form submit).
  • objEvent.preventDefault();
  • return( false );
  • }
  • );
  • }
  • );
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • jQuery Data Demo With Iterative Click
  • </h1>
  •  
  • <form>
  • <button>Show List Item</button>
  • </form>
  •  
  • <ul>
  • <li>
  • I am list item One. <a>close</a>.
  • </li>
  • <li>
  • I am list item Two. <a>close</a>.
  • </li>
  • <li>
  • I am list item Three. <a>close</a>.
  • </li>
  • </ul>
  •  
  • </body>
  • </html>

The magic sauce here is the way in which we locate the element to show once the button has been clicked:

 Launch code in new window » Download code as text file »

  • var jHidden = objConfig.Collection.filter( ":hidden:first" );

The filter() method takes our original collection and returns a sub-collection of elements that match the given selectors. This filter uses two selectors, :hidden, which finds all invisible elements (or Inputs of type=hidden), and :first, which limits the collection to the first match. Now, because jQuery methods are not destructive (meaning that they return a new jQuery stack leaving the original jQuery stack unaltered), we can be sure that this filter does not change our Collection variable in any way. Thanks to this behavior, we can call this filter() method each time the button is clicked and it will always be searching the same master collection.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jan 29, 2009 at 10:41 AM // reply »
4 Comments

Ben...this is incredible, and so are you. Thank you for everything.


Jan 29, 2009 at 10:43 AM // reply »
6,516 Comments

@Kevin,

Glad to help!


Jul 17, 2009 at 5:00 PM // reply »
1 Comments

This is great!

How would I apply this to two or more seperate lists?


Aug 26, 2009 at 8:39 AM // reply »
1 Comments

thanx its usefull


Sep 6, 2009 at 1:23 PM // reply »
1 Comments

This is great!

How would I apply this to two or more seperate lists?


Sep 15, 2009 at 1:53 PM // reply »
1 Comments

And in 1.3.2?? Do we have to resort to using .filter to get just the elements that have css visibility=hidden?


Sep 18, 2009 at 4:45 PM // reply »
6,516 Comments

@Richard,

It depends on what your code is doing I guess. You can always use the :hidden pseudo select in the main selector. I guess, I'm not sure what you're asking?


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 8:55 AM
Project HUGE: Trying Out A 4-Exercise Limit
Very informative. Thanks for the great post. ... read »
aha
Nov 22, 2009 at 7:42 AM
Using A Name Suffix In ColdFusion's CFMail Tag
Why not? ... read »
Nov 22, 2009 at 7:37 AM
Using A Name Suffix In ColdFusion's CFMail Tag
asd ... read »
Nov 22, 2009 at 4:30 AM
jQuery Live() Method And Event Bubbling
dasegtezr ... read »
Nov 22, 2009 at 4:03 AM
jQuery Live() Method And Event Bubbling
C_fieri ... read »
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »