Text to URL Slug Converter

Code


$(document).ready(function (){
    //If the slug field is empty, update it with the stripped version of the name field
    $("input[name='name']").focus(function(){
         if($("input[name='slug']").val().length == 0){
              $("input[name='name']").keyup(function(){
                   var title = $(this).val();
                   var slug = title.replace(/[^a-z0-9]+/ig, " ").trim().replace(/[\s]+/ig,"-").toLowerCase();
                   $("input[name='slug']").val(slug);
              });
         }
         else {
              $("input[name='name']").off("keyup");
         }
    });

    $("input").first().focus();
});
Back