Skip to content

Instantly share code, notes, and snippets.

@melo
Forked from cpinto/gist:305094
Created February 16, 2010 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melo/305397 to your computer and use it in GitHub Desktop.
Save melo/305397 to your computer and use it in GitHub Desktop.
Keep most recent but don't delete original option
<body>
<select id="teste">
<option>item 1</option>
<option>item 2</option>
<option>item 3</option>
<option>item 4</option>
</select>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.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.clone());
});
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment