Simple POC - Dynamically select an element from a list and skip the last chosen

This post is more than 2 years old.

Pardon the super-long title up there. This is based on a simple StackOverflow question from today (Select random list item and add class) based on a simple premise: Given a set of tags, how would you remove a class from one of them and dynamically re-apply it to another one chosen by random?

I chimed in, as did others, but I thought it might be interesting to share my solution here. I updated my code to make use of SessionStorage to remember the last item selected. As you know, I'm a big fan of LocalStorage and other "practical" HTML5 features, and I thought this was a nice, if simple, example of where you could use the API. Here's the code:

And you can run it on JSBin here:

There really isn't much to say about this. If anyone needs something explained, just ask. Again - I'm mainly sharing it as a practical example of LocalStorage.

P.S. As explained on StackOverflow, I intentionally made the code more verbose then it needed to be to help explain it to the user. Since we are still talking just 20 lines of code here, I don't think that's a big deal. ;)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA