Skip to content

Instantly share code, notes, and snippets.

@timstermatic
Created May 10, 2015 21:13
Show Gist options
  • Save timstermatic/a53cbe94320679a8f1e1 to your computer and use it in GitHub Desktop.
Save timstermatic/a53cbe94320679a8f1e1 to your computer and use it in GitHub Desktop.
populate one field with a url slug based on value entered into another.
(function ( $ ) {
$.fn.slugger = function(options) {
var that = this;
var settings = $.extend({
target: null,
}, options );
if(settings.target) {
settings.target.focus(function() {
$(this).addClass('dirty');
});
that.keyup(function() {
if(!settings.target.hasClass('dirty')) {
settings.target.val($(that).val().toLowerCase().replace(/[^\w\s]/gi, '-').split(" ").join("-"));
}
})
}
};
}( jQuery ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment