-
-
Save melo/305397 to your computer and use it in GitHub Desktop.
Keep most recent but don't delete original option
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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