Posted By

FatFolderDesigner on 02/27/11


Tagged

simple jquery alphabetical


Versions (?)

Insert alphabetically with jQuery


 / Published in: jQuery
 

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

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.

  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. }

Report this snippet  

You need to login to post a comment.