Skip to main content
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Clark Valberg
Ben Nadel at cf.Objective() 2014 (Bloomington, MN) with: Clark Valberg ( @clarkvalberg )

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

Published in , Comments (8)

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:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
	<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.
				var jButton = $( "button" );

				// First, hide all the list items.
				$( "li" ).hide();

				// Hook up the "close" links.
				$( "li a" )
					.attr( "href", "javascript:void( 0 )" )
							$( 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.
						Collection: $( "li" )

				// Now that we have our data bound, let's bind
				// the click event.
					function( objEvent ){
						var jThis = $( this );

						// Get the config data out of the button.
						var objConfig = "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.

						// Prevent default event (form submit).
						return( false );


		jQuery Data Demo With Iterative Click

		<button>Show List Item</button>

			I am list item One. <a>close</a>.
			I am list item Two. <a>close</a>.
			I am list item Three. <a>close</a>.


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

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.

Want to use code from this post? Check out the license.

Reader Comments



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?


Hi Ben,

how about rather "close"-links than a "back"-button to slideup the shown-elements? How it would be look? Thank you!


I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel