Insert alphabetically with jQuery


/ Published in: jQuery
Save to your folder(s)

This function will insert items into an orders alphabetically (assuming it's already alphabetical). Initially design to make a smooth single page load/save feature that sotred everything in drop down lists, you guessed it, alphabetically. Can simply be modified to work in any sort of layout (ordered lists was just easier for the example)

Example at the link, you can post any questions there too.


Copy this code and paste it in your HTML
  1. function insert(){
  2. var name = $("input[name='insertvalue']").val();
  3. if(name!=''){
  4. var toinsert = true;
  5. $("ol.thelist > li").each(function(){
  6. var item = $(this).html();
  7. if(name.toUpperCase() < item.toUpperCase()){
  8. if(toinsert){
  9. $(this).before('<li>'+name+'</li>');
  10. toinsert = false;
  11. }
  12. }
  13. });
  14. if(toinsert){
  15. $("ol.thelist").append('<li>'+name+'</li>');
  16. }
  17. $("input[name='insertvalue']").val('')
  18. }
  19. }

URL: http://fatfolderdesign.com/202/unimportant/insert-alphabetically-with-jquery

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.