Twitter: raymondcamden


Address: Lafayette, LA, USA

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

10-22-2012 3,916 views JavaScript, HTML5 Comments

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: http://jsbin.com/axudey/1/

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. ;)

0 Comments

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty