Skip to content

Instantly share code, notes, and snippets.

@cpinto
Created February 15, 2010 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cpinto/305094 to your computer and use it in GitHub Desktop.
Save cpinto/305094 to your computer and use it in GitHub Desktop.
dropdown with most recently clicked items
<body>
<select id="teste">
<option>item 1</option>
<option>item 2</option>
<option>item 3</option>
<option>item 4</option>
</select>
<script src="js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function()
{
$("#teste").change(function(o){
var selected = $(this).find("option:selected");
var mostrecent = $(this).find("optgroup#mostrecent");
if (mostrecent.length == 0)
{
mostrecent = $("<optgroup label='Mais recentes' id='mostrecent'/>");
$(this).prepend(mostrecent);
}
if (selected.parent().get(0).id == "mostrecent") return;
mostrecent.prepend(selected.remove());
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment