Return to Snippet

Revision: 42113
at February 27, 2011 15:14 by FatFolderDesigner


Initial Code
function insert(){
    var name = $("input[name='insertvalue']").val();
    if(name!=''){
        var toinsert = true;
        $("ol.thelist > li").each(function(){
            var item = $(this).html();
            if(name.toUpperCase() < item.toUpperCase()){
                if(toinsert){
                    $(this).before('<li>'+name+'</li>');
                    toinsert = false;
                }
            }
        });
        if(toinsert){
            $("ol.thelist").append('<li>'+name+'</li>');
        }
        $("input[name='insertvalue']").val('')
    }
}

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

Initial Description
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.

Initial Title
Insert alphabetically with jQuery

Initial Tags
simple, jquery

Initial Language
jQuery