Ask Ben: Iterating Over An Array In jQuery, One Index Per Click

Posted January 28, 2009 at 9:04 AM by Ben Nadel

Tags: Javascript / DHTML, Ask Ben

Ben, I'm desperate. I've been all over the Internet looking for some clue how to do this: How would I use .each() with .click to have a single button that would iterate through an array, one object at a time, each time it is clicked? I tried something like:

var myarray = [(#div0),(#div1),(#div2)];
$( myarray ).each(
$("#button").click(function(){
$("#"+objValue).slide("slow");
});

But, obviously that doesn't work. I need the one button, to slide one panel at a time, per each click, in succession. Does that make sense?

When I read your question, the first thing that immediately popped into my mind was jQuery's data() method. I discovered the data() method recently and it blew my mind! If you have not seen it before, jQuery's data() method allows us to associate arbitrary data with any of our DOM elements. What I'm thinking is that if we bind an "Index" value and the actual collection of jQuery elements to our button, then we can easily iterate over the collection, one index per click:

 
 
 
 
 
 
 
 
 
 

As seen in the video, each time we click the button, a new item in our unordered list is shown (using slideDown()). The code for this is quite straightforward:

  • <!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();
  •  
  • // Now, we are going to bind data to the button.
  • // This data will include the array of jQuery
  • // elements that we want to show and the index of
  • // the currently selected item.
  • jButton.data(
  • "config",
  • {
  • Index: 0,
  • 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" );
  •  
  • // Check to see if our index it out of
  • // bounds (more items to click).
  • if (objConfig.Index < objConfig.Collection.length){
  •  
  • // Show the current item. When we are
  • // doing this, post-increment the our
  • // item index.
  • $( objConfig.Collection[ objConfig.Index++ ] ).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.
  • </li>
  • <li>
  • I am list item Two.
  • </li>
  • <li>
  • I am list item Three.
  • </li>
  • </ul>
  •  
  • </body>
  • </html>

As you can see in the code, we are associating a "config" structure with our DOM element, the button. Then, whenever the button is clicked, we retreive that associated "config" data and use the contained "Index" and "Collection" data as needed. jQuery is so powerful, I'd be surprised if there wasn't an even easier way to do this; but, this was the first thing that popped into my mind.




Reader Comments

Jan 28, 2009 at 9:30 AM // reply »
4 Comments

You, sir, are the man. This works exactly as I had invisioned it. I'm gonna get the hang of jQuery. Some of it is actually starting to make sense. I've been doing this javascript the long way for so long, it's been difficult to adapt. But, I have to say, the sheer power of jQuery blows me away.

If I wanted to utilize more specifically the IDs of certain objects, instead of the list items, how would I do that?


Jan 28, 2009 at 9:33 AM // reply »
11,246 Comments

@Kevin,

If you wanted to use IDs, you could simply build the Collection element the same way you did in the question:

Collection: [$("#div0"), $("#div1"), $("#div2")]

The only difference now is that each element in the array contains a jQuery object. As such, when you reference later on, you wouldn't need to re-wrap it in the $() method:

$( objConfig.Collection[ objConfig.Index++ ] ).slideDown();

... vs. ...

objConfig.Collection[ objConfig.Index++ ].slideDown();

Notice that the latter does not use the $() before calling .slideDown(). This has to do with the fact that you are using a jQuery stack in the first and standard array in the second.


Jan 28, 2009 at 9:43 AM // reply »
4 Comments

Thank you very much. I'm going to spend a lot of time on your blog. Your articles have been the most productive learning on jQuery I've found. Keep up the good work...


Jan 28, 2009 at 9:54 AM // reply »
11,246 Comments

@Kevin,

Always glad to help. Please feel free to ask me for any demos - it's how I spend my mornings :)


Jan 28, 2009 at 10:36 AM // reply »
4 Comments

Ben,

Now I'm really going to get confusing.

Each of the DIVs in my array have "X" buttons, where I use slideUp() to remove them. If I have iterated through all of the array objects, then remove one or more of the DIVs, how can I "reset" the array so that I can click and add them back, if need?

And, while we're at it, I'd like to reduce the amount of code in my script by passing the ID of the DIV back to the slideUp() function, instead of having a separate function for each one. I do this all the time with long javascript, but not sure how I would accomplish this with jQuery. (Feel free to make this a separate post, if you prefer. I think it could be a useful tutorial on reusing code.)

Thanks,
Kevin


Jan 28, 2009 at 2:55 PM // reply »
11,246 Comments

@Kevin,

Hmm, interesting....


Jan 28, 2009 at 3:23 PM // reply »
11,246 Comments

@Kevin,

Try this:

http://www.bennadel.com/blog/1476-Ask-Ben-Displaying-The-Next-Hidden-Element-Using-jQuery-Update-.htm

The difference here is that you have to use a jQuery stack - you can't just use an array for your collection (since we need to filter on it). You can still add arbitrary elements to the same jQuery stack:

Collection: $("#div0").add("#div1").add("#div2")

Hope that helps.


May 15, 2009 at 11:13 AM // reply »
3 Comments

Hi Ben. Great script. It has very nearly helped me to get exactly what I need. However, I need to tweak it and I just can't get it to work. What I am looking for is a way to hide all but the first three items on load (as opposed to hiding all items). Then I need a way to reduce the number of items back down to a minimum of three. I am trying to reproduce something similar to the BBC home page (bbc.co.uk) where clicking the plus/minus buttons shows/hides additional items. Any help to finish this off would be most appreciated. I can email you the code I have so far if required?


May 19, 2009 at 9:34 AM // reply »
11,246 Comments

@John,

To hide everything after 3rd element, you can use the selector: gt():

$( "div:gt(2)" ).hide()

Since gt() is zero-based, greater than 2 will exclude 0, 1, and 2.


May 19, 2009 at 10:01 AM // reply »
3 Comments

Many many thanks, Ben. I never knew about using :gt in JQuery. That makes things a lot simpler...


Oct 19, 2009 at 2:38 PM // reply »
1 Comments

life saver....

was looking for a simple approach to showing and hiding individual profiles on a click of an anchor. This gave me a massive input.

Thanks


Oct 31, 2009 at 4:47 PM // reply »
11,246 Comments

@errol187,

Awesome :)


Apr 27, 2012 at 1:28 PM // reply »
1 Comments

I know this thread is a few years old but the script is doing exactly what I want to do. However, I need to create a backbutton as well that goes reverse through the divs.

I have created the same exact function but used ++backorder.Index for the backwards instead of forwardorder.Index++ for forwards. However, this is not working as expect. It seems that the two data arrays need to relate to eachother so they can tell where in the index each is.

Articulating this is helping but if anyone has any ideas, I am alle ears.

Thanks



Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:01 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Avi, Really glad to help! @Jaredwilli, I'm finding a this image hits home with a lot of people :) Hopefully we can all work through the rough patches together! @Prateek, AngularJS has error ... read »
May 25, 2013 at 9:53 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Mrsean2k, I'm glad I could help! I haven't been able to keep up with the ui-router stuff. I keep saying that I'll carve out time, but I just haven't gotten to it :( ... read »
May 25, 2013 at 9:49 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, Thanks for the book recommendations. I am looking them up right now. I can see that Object Thinking is available for the Kindle App - sweet! Also, I just recently heard Martin Fowler on the ... read »
May 25, 2013 at 9:41 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
@Chris, I'm super excited to hear that my posts are helpful. I am also loving AngularJS; but, it definitely has some caveats and some odd behaviors and some things that just don't seem to "wor ... read »
May 25, 2013 at 9:36 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam, @Jason, After reading these comments, I double-checked my latest implementation and I am happy to report that I am using listFirst() and listRest(). ... read »
May 25, 2013 at 9:31 PM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Daxesh, I am not sure I understand the question about the current node. If you already have a reference to the current node, why would you need to query for it? As for parent node, I believe that ... read »
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools